0

My divs include inputs of type select, text input, checkbox and various others

I have successfully managed to set the attribute of disabled for all elements in a div to true using the code below in jQuery.

$("#for_all_div1 :input").attr("disabled", true);
$("#for_all_div2 :input").attr("disabled", true);

My question is how can I set the attributes for all inputs in the div but skip the checkbox inputs.

Thanks,

2 Answers 2

2

If you want to disable all input except checkbox use :not(:checkbox)

$("#for_all_div2 :not(:checkbox)").prop("disabled", true);

Also, it's better to use .prop() rather than .attr() for setting the disabled attribute.

Demo

$("#for_all_div2 :not(:checkbox)").prop("disabled", true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="for_all_div2">
<input type="text" />
<input type="checkbox" />
</div>

Sign up to request clarification or add additional context in comments.

3 Comments

@DouglasHosea No problem, happy to help
p/s: You should be using .prop() to set properties, not .attr().
@Terry true true, I just copied the op's example, must admit i didnt check the .attr. will change it.
1

use .not()

$("#for_all_div2 :input").not("[type=checkbox]")
.attr("disabled", true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="for_all_div2">
<input type="text" /><br>
<input type="text" />

<input type="checkbox" />
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.