1

I am writing a jquery code which aim is to disable/enable group of checkboxes. So far I got: Html:

<select id="Units"> <option value="Marketing" > Marketing </option> <option value="Finance" > Finance </option> <option value="Operations" > Operations </option> </select> <input type="checkbox" class="enable" onclick="check();" value="1"> chbx1 <input type="checkbox" class="dissable" onclick="check();" value="2"> chbx2 <input type="checkbox" class="dissable" onclick="check();" value="3"> chbx3 <input type="checkbox" class="enable" onclick="check();" value="4"> chbx4 

JS

$("#Units").on("change", function() { if ($(this).val() !== "Finance") { $(".dissable").attr("disabled", "disabled"); } else { $(".dissable").removeAttr("disabled"); } }); 

But it doesn't work properly on the page load. Only after change the selected option the checkboxes react. How to improve this in order to catch selected value when the page is load and enabled/disabled checboxes with apropriate class?

1
  • Trigger the change event manually or set initial values in HTML Commented May 11, 2015 at 10:56

3 Answers 3

1
$(".dissable").attr("disabled", "disabled"); $("#Units").on("change", function() { if ($(this).val() !== "Finance") { $(".dissable").attr("disabled", "disabled"); } else { $(".dissable").removeAttr("disabled"); } }); 

demo: http://jsfiddle.net/zgn2o40a/1/

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

Comments

0

You can trigger the change event after defining the event using .change() or .trigger('change'):

$("#Units").on("change", function() { //rest code }).change(); 

Working Demo

Comments

0

Try this:

$("#Units").on("change", function () { if ($(this).val() !== "Finance") { $(".dissable").prop("disabled", true); // Use prop instead of attr } else { $(".dissable").prop("disabled", false); } }).trigger('change'); // ^ // This will call function immedietly 

1 Comment

Your code will attach undefined listener to change event

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.