views:

77

answers:

1

I want to be able to select the div next in line (its not a child div, or a parent div) more like a sibling. Then I want to get the node_id from that div and parse it. For example, below I know the "NODE_ID" of the first one, so I can use a jquery selector to get that node_ID, then I want to move directly beneath that DIV and retrieve the NODE_ID of that one, but I won't know the ID, or Node_id, so I have to be able to get to the next DIV without an id for a selector...in this case I want to retrieve the node_id value "74_3" and parse it.

Here's the HTML:

<div style="display: block;" id="71_2_sub_p_div" node_id="72_2">
    <span class="a_hand">
        <img src="../images/maximize.png"> &nbsp;Commercial
    </span>
</div>

<div style="display: block;" id="71_3_sub_p_div" node_id="74_3">
    <span onclick="toggle_display('75_4_sub_p_div');" class="a_hand">
           <img src="../images/maximize.png"> &nbsp;Apartments
    </span>
</div>

Here is the selector I'm using to get the Known NODE_ID's:

$("div[node_id='72_2']")

How can I do this in jquery? Thanks in advance.

+5  A: 

If the div is the next sibling, you can use .next()

$('div[node_id=72_2]').next(); // should be div#71_3_sub_p_div
$('div[node_id=72_2]').next().attr('node_id'); // should be '74_3'

If there are other siblings, and you only want to find <div node_id='...'>'s you can get a little more complicated:

$('div[node_id=72_2]').nextAll('div[node_id]').eq(0).attr('node_id') 
// also '74_3'

Looks through all the next siblings looking for divs with the node_id attribute, reduces the match to a single item using .eq() and then returns it's node_id attribute.

gnarf
Instead of using `.slice(0,1)` you should better use `.first()`: http://api.jquery.com/first/
kroimon
`.first()` just calls `this.eq(0)` so, I just used `eq(0)`, which interestingly just calls `this.slice(0,1)` so, its all the same, and `.slice()` requires 2 less function calls than `.first()`
gnarf
Great. This did it. Thanks for the help!
Ronedog
`first` returns a jQuery object of the first element; `get(0)` returns the DOM element itself.
Matt
@matt - Yes, I know this. `.eq(0)` and `.slice(0,1)` return a jQuery objects as well.
gnarf
Sorry... I'm just an idiot who can't differentiate between `eq` and `get` :P
Matt