tags:

views:

2788

answers:

1

Assume parentNode exists and I want to add an element "Child" to it. Following intutive code won't work:

$("<Child/>").appendTo(parentNode);

Because jQuery will create a node <CHILD> and append to parentNode.

So I am wondering, how do you add xml child node in jQuery?

p.s. Following ugly code will work, but it is really really ugly: parentNode.appendChild(parentNode.ownerDocument.createElement("Child"));

p.s.2 $(parentNode).append('<Child /&gt') won't append the child node with jQuery 1.2.6 on FireFox 3. Actually it append nothing. If use appendTo(), it will append a node with name CHILD (all capital).

+2  A: 

JQuery is not meant to treat xml. When you use $("<Child/>") JQuery uses a hidden div innerHTML to build the child node, that's why the capitalization differs.

Serhii
I don't think it's accurate to say that "JQuery is not meant to treat xml." JQuery has several methods for making AJAX requests. Since the "X" in AJAX stands for XML, JQuery ought to be able to deal with this situation.
dthrasher
But I agree that JQuery doesn't really have very good ways of manipulating XML at the moment.
dthrasher