JQuery multiple point selectors?

OK I'm a little noob ... but not that noob. :)

I want to do find () in jQuery, which has a similar dot notation or "& &" result. Here is an example (which does not work):

data.find("continent_country[id = 'us'].state[id = 'in']").each(function(){ // what to do } 

or

 data.find("continent_country[id = 'us'] && state[id = 'in']").each(function(){ // what to do } 

I was told to try a comma, for example:

 data.find("continent_country[id = 'us'], state[id = 'in']").each(function(){ // what to do } 

... but this returns the wrong elements.

My XML looks like this:

  <continent_country id="us" name="US"> <state id="al" name="Alabama"> <city url="" name="auburn"/> <city url="" name="birmingham"/> <city url="" name="dothan"/> <city url="" name="florence / muscle shoals"/> <city url="" name="gadsden-anniston"/> <city url="" name="huntsville / decatur"/> <city url="" name="mobile"/> <city url="" name="montgomery"/> <city url="" name="tuscaloosa"/> </state> <state>//more states</states> </continent_country> </continent_country id="eu" name="Europe"> <state>//more states</states> </continent_country> 

Some states / provinces / countries use the same identifier, so I would like to find the state in a certain continent.

Thanks in advance...

+4
source share
5 answers

I think you want a baby designation:

http://api.jquery.com/child-selector/

sort of

 data.find('continent_country[id = 'us'] > state[id='in']).each(function(){ //do your stuff here } 
+4
source

You can simply use the add() method, which adds more elements to the selection based on a new selector, for example

 data.find("continent_country[id = 'us']").add("state[id = 'in']").each(function(){ // what to do } 
+1
source

You can use the :has selector, the .has function .has or select the closest continent_country above the state. Here's a has function, which is probably the most efficient.

 data.find("continent_country[id='us']").has("state[id='in']") 

Suppose you want to get <continent_country> elements ... if I misunderstood and you want to get <state> elements, use a space:

 data.find("continent_country[id='us'] state[id='in']") 
0
source

Can you try the .has() method?

 data.find("continent_country[id = 'us']").has("state[id = 'in']").each(function(){ // what to do } 

That should work.

0
source

If you want to create a state inside the country, you must use the descendant selector continent_country[id='us'] state[id='in']

Or a child selector : continent_country[id='us'] > state[id='in']

You should learn more about jquery selectors.

-one
source

Source: https://habr.com/ru/post/1399810/


All Articles