Home > Enterprise >  JQuery add attribute function is not working
JQuery add attribute function is not working

Time:09-30

$('#section_option').change(function () {
      var selectedClass = $("#section_option option:selected").val();
      if (selectedClass != 0) {
        //alert("You have selected the class - "   selectedClass);
        $("#sectionname").removeAttr("disabled");
        $("#sectionname").removeClass("disabled");
      } else if(selectedClass == 0) {
        alert("0 selected");
        $("#sectionname").addAttr("disabled");
        $("#sectionname").addClass("disabled");
      }     
    });

First portion of code i.e removing of attr and class is working fine but when i am trying to lock the input again when user switches back to option val 0, it is not happening due to some reason.

CodePudding user response:

Not addAttr("disabled") is attr("disabled",true);

$('#section_option').change(function () {
  var selectedClass = $("#section_option option:selected").val();
  if (selectedClass != 0) {
    //alert("You have selected the class - "   selectedClass);
    $("#sectionname").removeAttr("disabled");
    $("#sectionname").removeClass("disabled");
  } else if(selectedClass == 0) {
    alert("0 selected");
    $("#sectionname").attr("disabled","disabled");
    $("#sectionname").addClass("disabled");
  }     
});

Or you can use prop:

$("#sectionname").prop("disabled",true);
  • Related