I'm having some weird behavior from my jQuery. I am running jQuery 1.10.2 from google's ajax api.
According to the jQuery documentation the .html() function is supposed to call the function .empty() before inserting the text. My problem is that I have a div container with a unique id and when I insert text using the .html() function the default text is cleared and my desired text is inserted, which is supposed to happen.
<div id="xmlContent">
this is a test <!-- Default Text -->
</div>
But if I click my "add text button" again the new text is just appended to the previously generated content instead of replacing it. Further still if I keep clicking on the button the new content nests within the first child element.
<div id="xmlContent">
<code>
<code>
<name>fred<br>
<address></address><br>
<issitelocation></issitelocation><br>
</code>
<name>fred</name><br>
<address>addres</address><br>
<issitelocation></issitelocation><br>
</div>
On a related topic, .empty() clears the default text, but refuses to clear any generated content.
I have the code in a jsfiddle right here here.
Also the code is here
function generateXML() {
xmlHTML += '<name>' + $('#locationName').val() + '</name><br />';
xmlHTML += '<address>' + $('#address').val() + '</address><br />';
xmlHTML += '<isSiteLocation>' + $('#isPathwaySite').val() + '</isSiteLocation><br />';
xmlHTML = xmlHTML.replace('<','<').replace('>','>');
xmlHTML = '<code>'+xmlHTML+'</code>';
}
function populateXMLContainer() {
$('#xmlContent').html(xmlHTML);
}
$(function() {
$('#generateXML').click(function() {
generateXML();
populateXMLContainer();
});
});
Please help, I have no idea what I'm doing wrong.
Clarifying Note
The reason for the < and the > is because I'm trying to create an xml generator for myself.
xmlHTML = ''in the first line ofgenerateXMLmethod.. Sincevar xmlHTMLis being shared , it holds the previous values that is being appended in the first line