0

Excuse the simple question: I'm fairly new to JQuery!

I have a variable named xmlText containing an XML string chunk where I'm sure that there is only one node of type "node1", and so on.

<root><node1>value1</node1><node2>value2</node2></root>

Using JQuery I can access the value of a given node like this:

$('root node1', xmlText).each(function () {
 alert($(this).text();

});

How can I access the value witout a ".each(function()" construct?

Notice the xmltext variable is just a text chunk, xml like formated, not a complete well formated xml file nor a parsed DOM object.

3 Answers 3

1

each is used to loop over a collection but you can simply apply the function if you only have one element.

Simply do

$('root node1', xmlText).text()
Sign up to request clarification or add additional context in comments.

1 Comment

@distroy Thanks, this is the most straightforward, simplest syntax.
0

Try this:

 $(xmlText).find('root node1').text();

Comments

0

Try this:

var xmlText = $('<root><node1>value1</node1><node2>value2</node2></root>');
alert(xmlText.find('node1').text());

jsFiddle

Comments

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.