2

i want to add new attribute in XML using jquery

for e.g: XML:

new XML i want display as:

<data>
<section marked="marked"></section>
</data>

from above i want to add marked="marked" attribute using jquery.

1

1 Answer 1

2

You can add attribute in any XML tag as you add in HTML

First your XML is saved in any variable

var a = '<data><section></section></data>';

Edit

Now you have to add this line too and it will work fine.

a = $(a);

This line will add an attribute in section tag

a.find('section').attr('marked', 'marked');
Sign up to request clarification or add additional context in comments.

4 Comments

hi i've tried above code but its giving '[object Object]' output.
@Jay Please check my updated answer. Sorry for missing the edited line.
i trying with ' var a = '<data><section></section></data>'; a = $(a); a.find('section').attr('marked', 'marked'); alert(a);' but same output :(
@Jay Yeah because after doing a = $(a) the variable a is now jQuery Object. To get modified xml you have to do this a[0].outerHTML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.