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

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

2 Comments

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
Check this out 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.