0

I try to disable/enable a textbox depending on the input other textbox has. When role textbox's input is _teacher_, the the subject textbox is enable. Otherwise, disabled.

Problem is it does not enable it back when the input is teacher

function check(event) {
    if ($('#role').val().toLowerCase() == "teacher")
            $("#subject").removeAttr("disabled"); 
    else
        $("#subject").attr("disabled", "disabled"); 
}

view.tpl

<input type="text" class="input-block-level" id="role" onkeypress="check(event)" placeholder="Teacher/HR" name="role"/>
<br/>
<input type="text" class="input-block-level" id="subject" placeholder="Subject(only if Teacher) " name="subject"/>
<br/>
Lulu
  • 175
  • 1
  • 2
  • 12
  • There isn't enough information here to give a definitive answer. Please show more of your code, including the HTML for the elements and how you've bound the check function to the form. – JJJ May 30 '15 at 20:21
  • @Juhana I just added the html part – Lulu May 30 '15 at 20:24
  • 2
    Use the `onkeyup` event instead (or better yet, bind the event with jQuery, not inline.) – JJJ May 30 '15 at 20:28

1 Answers1

0

Try $("#subject").prop("disabled", false); to enable and $("#subject").prop("disabled", true); to disable the element

Alex Tartan
  • 6,736
  • 10
  • 34
  • 45
  • That is equivalent to adding and removing an attribute. – JJJ May 30 '15 at 20:27
  • 1
    same thing happens :( it does get disabled but remains like that even if the input in "teacher" :( – Lulu May 30 '15 at 20:28
  • @Juhana No, it's not. – Ram May 30 '15 at 21:10
  • @Juhana What makes it a particular case? `disabled` is a property, using `prop` is more reliable than setting/removing the attribute. – Ram May 30 '15 at 21:12
  • @Vohuman I don't think we use the same definition for the word "particular". In any case, feel free to show a version of the OP's code that works with `.prop()` but doesn't work with `.attr()`. – JJJ May 30 '15 at 21:15
  • @Juhana Well, the point wasn't whether it fixes the OP's problem or not. Using `prop` is not equivalent to "adding and removing an attribute", there is no guarantee that browser will update the corresponding property. There are many related questions on SO. http://stackoverflow.com/questions/5874652/prop-vs-attr – Ram May 30 '15 at 21:20
  • @Vohuman Ok, I agree, but I never said (or meant to say) that `.prop()` and adding/removing *any* attribute is *always* equivalent. I was commenting why *this specific* answer is wrong, because (implicitly) in *this* case they are equivalent. – JJJ May 30 '15 at 21:23