css - Correct way to disable html form elements using jQuery -


i have following code finds button elements html5 data-attribute called data-button-day has values of days of week.

for example

<button type="button" data-button-day="sunday" class="btn btn-danger btn-block sunday"> 

what do, when button clicked, elements data-attribute of data-day="dayofweek" either disabled or enabled depending on class of button.

<script> // finds correct button , works expected      $( "button" ).click(function() {         $( ).toggleclass( "btn-default btn-danger" );         var day = $(this).data("button-day");         toggle(day);     });  called, produces error isn't right code use      function toggle(day) {         $('[data-day="' + day + '"]').each().disabled();     } </script> 

i

typeerror: undefined not object (evaluating 'b.call')

you don't need .each() , each requires parameters. also, drop "div" selector.

to toggle behavior add/remove disabled class.

$("button").click(function () {      $(this).toggleclass("btn-default btn-danger");      var day = $(this).data("button-day");      var isdisabled = $(this).hasclass("disabled");      if (isdisabled) {          $(this).removeclass("disabled");      }      else {          $(this).addclass("disabled");      }      toggle(day, !isdisabled);  });      function toggle(day, disabled) {      $('[data-day = "' + day + '"]').prop("disabled", disabled);  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <button type="button" data-button-day="sunday" class="btn btn-danger btn-block sunday">sunday</button>  <input type="text" data-day="sunday" />  <input type="text" data-day="monday" />


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -