Check XML node for value using JQuery

I am trying to check if a value exists in xml node using JQuery. Xml string:

        <SectionAnswers>
            <SectionAnswer>
                <detailID>2216</detailID>
                <SourceAnswerID>979</SourceAnswerID>
            </SectionAnswer>
            <SectionAnswer>
                <detailID>2218</detailID>
                <SourceAnswerID>981</SourceAnswerID>
            </SectionAnswer>
            <SectionAnswer>
                <detailID>2219</detailID>
                <SourceAnswerID>977</SourceAnswerID>
            </SectionAnswer>
            <SectionAnswer>
                <detailID>2221</detailID>
                <SourceAnswerID>980</SourceAnswerID>
            </SectionAnswer>
            <SectionAnswer>
                <detailID>2282</detailID>
                <SourceAnswerID>64</SourceAnswerID>
            </SectionAnswer>
            <SectionAnswer>
                <detailID>2283</detailID>
                <SourceAnswerID>978</SourceAnswerID>
            </SectionAnswer>
            <SectionAnswer>
                <detailID>2596</detailID>
                <SourceAnswerID>73</SourceAnswerID>
            </SectionAnswer>
        </SectionAnswers>

      

When I try to query it for a value using the following:

$ ("SectionAnswers", section) .find ("64") // Section is the jquery context

I get the following response:

The expression does not return a DOM node.

.//--> 64 <-

Any ideas where I'm going wrong? I really don't want them to check the value every time like in $ ("SectionAnswers", Section) .each ()

thank

+2


source to share


4 answers


Try to traverse the XML using a simple $ .each:

$('SectionAnswers > SectionAnswer').each(function() {
    if($(this).find('SourceAnswerID').text() == '64') {
        alert('Found 64 at detailID: ' + $(this).find('detailID').text());
    }
});

      



or using filter :

var $sa = $('SectionAnswers > SectionAnswer').filter(function() {
    return $(this).find('SourceAnswerID').text() == '64'; 
});
alert($sa.find('SourceAnswerID').text());
alert($sa.find('detailID').text());

      

+4


source


I'm leaving this here for reference, as it can be useful in slightly different circumstances, but as karim79 mentioned it matches anything that has 64 as a substring.


You can use the ": contains (text)" pseudo selector :



$("SectionAnswers SourceAnswerID:contains('64')", Section)

      

This will select the SourceAnswerID members, so you may need to use a function parent()

or closest()

to navigate the hierarchy.

+1


source


Thanks guys.

I'll play around with both of them and see what I can think of. I want to avoid the loop if possible, because it will already run inside the loop and I just have a button about nested loops ...

But now that I think about it ... Both the filter and the calls are always inner loops ... So it could perform better just by just getting the collection and iterating over it.

0


source


OK got the fix. I ended up changing the xml to make the ID attributes to be

        <SectionAnswers>
            <SectionAnswer SourceAnswerID="1487"
                           detailID="1420" />
            <SectionAnswer SourceAnswerID="1488"
                           detailID="2039" />
        </SectionAnswers>

      

And now I can find it through

find ("SectionAnswer [SourceAnswerID = +1487]")

This is the best solution because adding information in the attributes reduces the size of the return value.

0


source







All Articles