Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: finding previous div

Tags:

jquery

<div class="a" style="display:none">Content 1</div>
<div class="a" style="display:none">Content 2</div>

some other HTML...

<span id="b">foobar</span>

How can I match the first div class="a" above the span id="b" to show() it? The id="b" is the only thing I know before.

like image 500
powtac Avatar asked Dec 23 '22 05:12

powtac


2 Answers

Like this:

$('#b').prevAll('.a:first').show();
like image 192
SLaks Avatar answered Jan 08 '23 00:01

SLaks


$("#b").closest(".a").show();

Try that.

like image 23
brettkelly Avatar answered Jan 08 '23 00:01

brettkelly