SoldierCorp
SoldierCorp

Reputation: 7700

jQuery: How to get div where an element is contained?

I need to get the id of div where the input is contained, for example:

<div id="example">
    <label for="i1">Label1</label>
    <input id="i1" name="i1" type="text"/>
</div>

I hope can help me!

Upvotes: 3

Views: 106

Answers (3)

Ram
Ram

Reputation: 144659

You can try the :has() selector:

$('div:has("input")').attr('id')

DEMO

Upvotes: 2

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

Do you mean:

$("div:has('input')").attr("id");

Or

$("div").has('input').attr("id");

Upvotes: 1

Alex Ball
Alex Ball

Reputation: 4474

$('input').on('click', function(){

    alert($(this).parent().attr('id'));

});

Upvotes: 2

Related Questions