Ajinkya
Ajinkya

Reputation: 22720

Nested selector in Jquery next

I have following HTML

<div id="header1" class="toggle">Section 1 : Create an account</div>
<div id="section1">
    First name: <input type="text" name="firstname" /><br />
    Last name: <input type="text" name="lastname" />
</div> 

I want to disable all input elements which are in a div which is next to div with class="toggle".
In above exmple div with id="header1" have class="toggle",I want to disable all inputelements which are in div next to it i.e. input elements within div with id="section1".

I tried $('.toggle').next(div input)).attr("disabled", true); but no luck. I am not sure if we can use nested selector in next().

Upvotes: 0

Views: 236

Answers (2)

Richard Dalton
Richard Dalton

Reputation: 35793

Use next and then find:

$('.toggle').next('div').find('input').attr("disabled", true);

http://jsfiddle.net/b8n2k/1/

Or using an expanded version of 3nigmas answer:

$('div.toggle+div input').attr("disabled", true);

http://jsfiddle.net/b8n2k/

Upvotes: 2

Rafay
Rafay

Reputation: 31043

See Next Adjacent Selector (“prev + next”)

$("div.toggle+div").find(":input").prop("disabled",true);

Upvotes: 2

Related Questions