Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use JQuery to get all nodes with an attributes equal to a value?

Tags:

jquery

xml

I am getting some XML back from an AJAX call (no surprise) and I want to do something but only on certain nodes and something else on the rest. For example

<xml>
  <node name="x">
  </node>
  <node name="x">
  </node>
  <node name="y">
  </node>
  <node name="z">
  </node>
</xml>

I want all the nodes with name x to go to one table and I want all the others to go to another table.

like image 876
uriDium Avatar asked Jun 24 '09 20:06

uriDium


1 Answers

Use an attribute filter, in particular the attributeEquals filter:

$("node[name='x']");

To select all the other nodes, use the attributeNotEquals filter:

$("node[name!='x']");

You can then apply jQuery manipulations to move these nodes elsewhere.

Note that XPath-style selectors where deprecated in version 1.2, and have been removed altogether in jQuery 1.3.

If you can influence what the server sends, you may want to switch to using JSON instead, you may find it easier to parse.

like image 162
Martijn Pieters Avatar answered Oct 12 '22 01:10

Martijn Pieters