0

Ok, so I'm pretty much a noob at Javascript, and totally have no idea about XML, so sorry if this is a dumb question.

I have a Javascript file (created by Dymo, the printer company) that will print from a browser to a label printer. I can manually change the information that gets printed. However, I would like it so that it would print whatever information is provided by an HTML form input field, filled by the user.

The problem is the printed data comes from XML within the Javascript.

What I want to know is how I can take the data that the user types into the form, and place it into XML so that I can then print that data. I realize this is a semi-broad question, and potentially difficult.

The Form:

<form name="myform" onsubmit="return false">
    QR Data Insertion: <input type="text" name="mytextfield" onchange="checkForm()" autofocus>
</form> 

The Javascript (pretty much unnecessary):

<script type="text/javascript" language="JavaScript">

function checkForm() { 
    var field1 = document.forms['myform'].elements['mytextfield'].value; 
    alert(field1);
}    

</script>

Not sure it will entirely be helpful, but below is a snippet of the XML:

<StyledText>\
    <Element>\
        <String>HERE IS WHERE I WANT THE FORM DATA TO GO</String>\
        <Attributes>\
            <Font Family="Arial" Size="12" Bold="False" Italic="False" Underline="False" Strikeout="False" />\
            <ForeColor Alpha="255" Red="0" Green="0" Blue="0" />\
        </Attributes>\
    </Element>\
</StyledText>\

1 Answers1

0

You can use DOM manipulation in your javascript function like this:

// Load the XML document
var xmlDoc = loadXMLDoc('yourXML.xml');
// Find the first String element
var x = xmlDoc.getElementsByTagName('String')[0];
// Change the content of that element
x.textContent = field1;

To load the document you can use this approach http://www.w3schools.com/dom/dom_loadxmldoc.asp, but I am not sure whether all the browsers support that.

Hope this helps!

  • Honestly, I'm not sure what DOM manipulation is. Not to mention the XML is contained within the JS file, so that complicates things. I will certainly be looking into this, though. Thank you – Andrew Terpening Nov 14 '14 at 21:38
  • Check this out http://www.w3schools.com/xml/xml_parser.asp. If you have the XML as String or as a loaded XML you can always modify it like this. – Vladislav Belichev Nov 15 '14 at 09:42