Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
87
rated 0 times [  93] [ 6]  / answers: 1 / hits: 26169  / 15 Years ago, mon, july 6, 2009, 12:00:00

chaning xml attributes through jquery is easy-peasy, just:



$(this).attr('name', 'hello');


but how can I add another tag into the file? I tried using append the JS dies silently.



Is there any way to do this?



Clarifications: this code is part of an extension to firefox, so don't worry about saving into the user file system. Still append doesn't work for xml documents yet I can change xml attribute values


More From » jquery

 Answers
251

The problem is that jQuery is creating the new node in the current document of the web page, so in result the node can't be appended to a different XML Document. So the node must be created in the XML Document.



You can do this like so



var xml = $('<?xml version=1.0?><foo><bar></bar><bar></bar></foo>'); // Your xml
var xmlCont = $('<xml>'); // You create a XML container
xmlCont.append(xml); // You append your XML to the Container created in the main document

// Now you can append without problems to you xml
xmlCont.find('foo bar:first').append('<div />');

xmlCont.find('foo bar div'); // Test so you can see it works

[#99186] Tuesday, June 30, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
maxh

Total Points: 137
Total Questions: 100
Total Answers: 103

Location: Kazakhstan
Member since Mon, Sep 26, 2022
2 Years ago
;