Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
9
rated 0 times [  13] [ 4]  / answers: 1 / hits: 76886  / 11 Years ago, wed, may 8, 2013, 12:00:00

I have a really simple problem.



How can I find the first previous element of another element? I tried the code below without success.



HTML:



<div class = A>HERE</div>

<div class=B>
<div class=C style=width:50px;height:50px;background-color:#000000;></div>
</div>


JS:



$('.C').click(function() {

alert($(this).closest('.A').html());

});


Fiddle: http://jsfiddle.net/Mcujp/4/


More From » jquery

 Answers
6

If you are trying to get the preceding sibling, use .prev().



If you are trying to get the sibling of the parent (like in your example) use .parent().prev().


[#78346] Tuesday, May 7, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jabari

Total Points: 580
Total Questions: 85
Total Answers: 110

Location: Western Sahara
Member since Mon, May 3, 2021
3 Years ago
;