We help IT Professionals succeed at work.

jQuery: select previous image to clicked button

Brad Bansner
Brad Bansner asked
I have an image and button within a DIV:

<img class="ranknum" src="art/gr_number_1.jpg" width="200" height="240" border="0" /><br />
Question 1<br />
<input type="button" class="rank clickable" value="Select">

I'm trying to select the 'src' attribute of the image directly preceding the clicked button (there are multiple DIVs like this on the same screen). I tried this, but its returning "undefined":

var selectProduct=$(this).prev('.ranknum').attr('src');

How can I get the 'src'? Thank you!
Watch Question

Try this

var selectedProduct=$(this).prevAll('.ranknum').attr('src');

Open in new window

Since .prev() get AN immediately preceding sibling which is <br/>, you cannot get src attribute.
Brad BansnerWeb Developer


Yes, that is all is took. Thanks!