3

How would i find the xml node that has a certain text value?

example i want to do something like this which is not working:

var xml = "<data><itemB>more data</itemB><itemC>yes, more data</itemC><itemA>some data</itemA></data>";

var searchString = "more data";

var foundin = $(xml + ":contains(" + searchString + ")");

3 Answers 3

3

You can invoke the :contains selector with the root XML element as its context:

var foundin = $(":contains(" + searchString + ")", xml);

You can see the results in this fiddle.

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

Comments

1

Try $(xml).children('*:contains("more data")')

Comments

1

These guys beat me to it, but here is the example I made.

    <script>
        $(function(){
            var xml = "<data><itemB>more data</itemB><itemC>yes, more data</itemC><itemA>some data</itemA></data>";
            var query = "more data";
            $(":contains(" + query + ")", xml).each(function(){
                alert(this.localName); //show node name
            });
        });
    </script>

Edit I'm assuming you're trying to get to the xml node with the query text. You probably want to use nodeName instead of localName for better IE support.

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.