Question

I have this xml:

<Checklist>
<ChecklistGroup id="" val="1" seq="" ilid="">
    <name>Group1</name>
    <ChecklistItem id="" val="1" seq="1" pcid="" piid="">
        <name>sb1</name>
    </ChecklistItem>
    <ChecklistItem id="" val="1" seq="2" pcid="" piid="">
        <name>sb2</name>
    </ChecklistItem>
    <ChecklistItem id="" val="1" seq="3" pcid="" piid="">
        <name>sb3</name>
    </ChecklistItem>
</ChecklistGroup>
</Checklist>

There can be a lot of ChecklistGroup nodes. How do I select the ChecklistGroup node based on the text of the next node which is 'name'. I want to select the checklistgroup node where it has a name of 'Group1'

How do i do that in javascript/jquery? Thanks in advance.

Was it helpful?

Solution

You can use something like below where $xml is the jQuery object reference to the xml

var $grp = $xml.find('ChecklistGroup > name').filter(function () {
    return $.trim($(this).text()) == 'Group1'
}).parent()

Note: :contains() was not used since it can return partial matches

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top