I want to add an attribute to an element tag, but I don't see how to do it programmatically. Is it possible? Also, is there a way to name an element tag dynamically? For example:
The way to add a child element is to either use the appendChild method like this:
XMLObject.appendChild(<childElement/>);
XMLObject.childElement= "something";
or just add it like this:
XMLObject.childElement = "something";
Right? (Side Question: Is there a difference between these two methods?).
What I want to do is to either add a dynamic attribute to a statically named child, or to create a dynamically named child.
I haven't been able to figure this out myself. Any ideas?
I think I'm on to something. Using eval(), I can add a dynamically named child, I think. It's working so far. Hope this is helpful for someone else.
XMLObject = new XML(<xml/>);
eval("XMLObject.appendChild(<" + nameReference + ">/);");
Now, I'll make this a function so I don't have to keep writing it over and over again. I imagine I can reference the node in a similar way . . . I hope.
Maybe have a look at Mozilla's E4X tutorial: https://developer.mozilla.org/en/E4X_Tutorial/
There's no need for eval() here. (There usually isn't.) Use:
XMLObject.appendChild(<{nameReference}/>);
To add an attribute, you can just use:
XMLObject.@new_att = "the value";
Jeff
North America
Europe, Middle East and Africa
Asia Pacific