Hi I'm trying to create a XML from an for-loop. But with this below code I only get the Last element found on the page. Not all of them as I expected.
Consider that there are many elements on the page with id's like "textHolder1, textHolder2 etc. With all different content.
Jquery:
var text = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><canvas>";
text += "<elements>";
text_length = $('[id^="textHolder"]').length;
for(var n = 0; n < text_length; n++){
TextElementID = $('[id^="textHolder"]').attr('id');
TextElementContent = $('[id^="textHolder"]').text();
text += "<element id='"+TextElementID+"'>";
text += "<content>"+TextElementContent;
text += "</content>";
text += "</element>";
}
text += "</elements>";
text +="</canvas>";
alert(text)
What am I doing wrong here?
$('[id^="textHolder"]')on each iteration, which selects ALL elements that match the selector. You are then getting the id of the first matched element and the content of the first matched element. That obviously isn't what you want.eqselector.