Victor Victor - 2 months ago 14
CSS Question

Parent/Closest Div Issue

EDIT:
I have the following code:

<div class="specificBlock" id="thevagabond">
<a class="intro"><img src="images/blabla.png"/></a>
</br>
<div class="btn-group-xs testing">
<a type="button" class="btn btn-success intro">Use</a>
</div>
</div>

<div class="specificBlock" id="thedog">
<a class="intro"><img src="images/blabla.png"/></a>
</br>
<div class="btn-group-xs testing">
<a type="button" class="btn btn-success intro">Use</a>
</div>
</div>


My issue is the following:
I'd like to find a jQuery/JavaScript way to get the ID of the div from which the clicked 'a' class="intro" comes from.

The problem here is that if the first
a
with
class="intro"
is clicked on, I would need to use the closest div's ID.

But if I click on the second the closest div is not the right one - the closest would be then the div with
class="testing"
, which I don't want.

Is there any way to do this?

Answer

ID should be unique, so you would do just $('#thevagabond'). However, you probably need to use .parents(selector_here) - check documentation.

If you really need ID, check this:

$('.specificBlock a').click(function(){
var id_that_you_looking_for = $(this).parents('.specificBlock').attr('id');
//code more
});