tags:

views:

329

answers:

4
+2  Q: 

jQuery Selector

I want to achieve going to the parent element then to the prev element get the atrribute id of the element which has class: classname.

<div>

<span><span id="190" class="classname">blabla</span></span>

<span><a href="#" class="button">blabla</a></span>

</div>

Pseudo code:

$('.button').click(function(){
 console.log($(this).parent().prev().$(".classname").attr("id"));
});

Do I have to use a find here or is there another way?

A: 

I would use find as you have suggested.

Garry Shutler
Thank you find it is then
+4  A: 

For your example:

$(this).parent().prev().children( '.classname' ).attr( 'id' );
tvanfosson
A: 

Find seems like the shortest way to get there....

alert($(this).parents("div").find(".classname").attr( 'id' ));

or

alert($(this).parents("div").find("span span").attr( 'id' ));
CarolinaJay65
+1  A: 

By the way, the id "190" is invalid, id and name attributes must begin with a letter, not a digit.

bendc