Shwetanka
Shwetanka

Reputation: 5036

jquery combining multiple selectors

alert($('.input-large[name="name"]:parent:parent').html());

My html is :

<div class="control-group">
    <label for="name" class="control-label">Clinic Name</label>
    <div class="controls">
       <input type="text" name="name" id="name" class="input-large" value="">
    </div>
</div>

I'm getting null in the alert. How to find the parent of the parent of the input element?

Upvotes: 0

Views: 212

Answers (3)

Matthew Blancarte
Matthew Blancarte

Reputation: 8301

You could do the parent().parent() chain as others have done, or just pass the parent selector into .parents() for a cleaner chain, like so:

alert( $('#name').parents('.control-group').html() );

Upvotes: 1

Keir Simmons
Keir Simmons

Reputation: 1684

For all input-large[name=name] elements:

$(".input-large[name=name]").each(function(){
    alert($(this).parent().parent().html());
});

But then again this will give you an alert for each instance.

Upvotes: 1

Zoltan Toth
Zoltan Toth

Reputation: 47667

Try

alert( $('#name').parent().parent().html() );

Upvotes: 2

Related Questions