1

Let's say I have the following very simple XML fragment:

<assignmentOptions>
    <MessageCharacterLimit value="4000" type="integer"></MessageCharacterLimit>
</assignmentOptions>
<options>
    <MessageCharacterLimit value1="1000" type="integer"></MessageCharacterLimit>
    <MessageCharacterLimit value2="2000" type="integer"></MessageCharacterLimit>
</options>

How can I find all MessageCharacterLimit under <options>? MessageCharacterLimit is not unique, but options/MessageCharacterLimit is.

Thanks

1 Answer 1

1

You can use querySelectorAll() for this:

var a = document.querySelectorAll('options > MessageCharacterLimit');
console.log(a)
<assignmentOptions>
    <MessageCharacterLimit value="4000" type="integer"></MessageCharacterLimit>
</assignmentOptions>
<options>
    <MessageCharacterLimit value1="1000" type="integer"></MessageCharacterLimit>
    <MessageCharacterLimit value2="2000" type="integer"></MessageCharacterLimit>
</options>

You may then loop over the collection you get like this:

var a = document.querySelectorAll('options > MessageCharacterLimit');
for (var i = 0; i<a.length; i++) {
    console.log(a[i].getAttribute('type'));
}
<assignmentOptions>
    <MessageCharacterLimit value="4000" type="integer"></MessageCharacterLimit>
</assignmentOptions>
<options>
    <MessageCharacterLimit value1="1000" type="integer"></MessageCharacterLimit>
    <MessageCharacterLimit value2="2000" type="integer"></MessageCharacterLimit>
</options>

Sign up to request clarification or add additional context in comments.

1 Comment

Just curious. if I do this: $(a).each(function () { ...... }); then for the a that has just one element each function is called twice. Any idea?

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.