Sathya
Sathya

Reputation: 5308

jquery selector by child tag value

see below code.

xml value

<root>
   <item>
      <name>name 1</name>          
      <class>A</class>
      <age>15</age>
    </item>
   <item>
      <name>name 2</name>         
      <class>A</class>
      <age>15</age>
    </item>
   <item>
      <name>name 3</name>         
      <class>B</class>
      <age>15</age>
    </item>
   <item>
      <name>name 4</name>         
      <class>A</class>
      <age>16</age>
    </item>
   <item>
      <name>name 5</name>
      <class>B</class>
      <age>15</age>
    </item>
</root>

i want to retrieve items that are having class = "A" AND age = 15. How can i we do this using jQuery selector.

something like this

$items = $(root).find("item age='25'&&class='A'").

Upvotes: 1

Views: 116

Answers (3)

Rohit Agrawal
Rohit Agrawal

Reputation: 5490

I understood your problem here what you can do-

var $items = [];
$('age').each(function(){
 if($('age').html()=="15"){
   $item = $(this).parent();
   if($('class',$item).html()=="A")
      $items.push($item);
 });

now $items is array of all items that are having class = "A" AND age = 15.

Upvotes: 1

JoeFletch
JoeFletch

Reputation: 3960

I did this.

$("item").filter(function(i){
   return $(this).find("class").text()=="A" &&  $(this).find("age").text()=="15"
       });

jsFiddle

Upvotes: 2

Johan
Johan

Reputation: 35194

var $items = $(root).find('item').filter(function() {
    return $(this).find('age').text() == 25 && $(this).find('class').text() == 'a';
})

Upvotes: 2

Related Questions