views:

4792

answers:

4

Hey,

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

A: 

I'd suggest you walk through the code with a debugger and see if you can determine why the append is causing an error (or if the error is someplace else). Something like:

$('selector').append('<p></p>');

should work just fine.

acrosman
there is no error, but the function stops executing.
Radagaisus
Have you tried the related manipulators like prepend() and insertAfter()?
acrosman
A: 

You'll have to send your altered xml back to a web service for saving server-side. Would you want client side javascript saving files to your file system?

ScottE
I'm developing a firefox extension.
Radagaisus
A: 

You can create elements simply by $('<div></div>'), or even $('<div />') , and adding them to your document using any of the Manipulation Methods. This should work for xml too, though I've only used it in the DOM so far.
Of course, you cannot save an XML file from javascript.

Kobi
this works great for the DOM, but not on the XML file.
Radagaisus
+1  A: 

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
Christian Toma
Raybiez