TSNev
TSNev

Reputation: 163

jQuery multiple selectors with dot notation?

OK. I'm somewhat of a noob... but not that noob-ish. :)

I desire to accomplish a find() in jQuery that has a similar result of dot notation or "&&". 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 have been told to try a comma, like so:

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

... but that returns the wrong items.

My XML looks like this:

    <continent_country id="us" name="U.S.">
        <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 share the same id, which is why I would like to find a state in a specified continent_country.

Thanks in advance...

Upvotes: 4

Views: 791

Answers (5)

soju
soju

Reputation: 25312

If you want a state inside a country you should use a 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

Upvotes: -1

Patricia
Patricia

Reputation: 7802

i think you want the child notation:

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

something like

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

Upvotes: 4

shanabus
shanabus

Reputation: 13115

Could you try the .has() method?

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

That should work.

Upvotes: 0

Ry-
Ry-

Reputation: 225124

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

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

That's assuming you want to get the <continent_country> elements... if I misunderstood and you want to get the <state> elements, use a space:

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

Upvotes: 0

danwellman
danwellman

Reputation: 9273

You could just use the add() method which adds more elements to the selection based on a new selector, e.g.

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

Upvotes: 1

Related Questions