Link to home
Start Free TrialLog in
Avatar of earwig75
earwig75

asked on

How can I enable / disable a text box with a checkbox

I have a text box that I'd like to disable when a checkbox is checked. Can someone offer sample code that would disable the input box when the checkbox is checked, and then enable it when it's unchecked? I can use javascript or jquery.

Thank you.

<input name="myInput" id="myInput" maxLength="10" type="text" /> 
<input name="myCheckBox" id="myCheckBox" type="checkbox" />

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mukesh Yadav
Mukesh Yadav
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of earwig75
earwig75

ASKER

Thank you that works great. Can I add an alert to the same function that will only alert when it's checked, and not when you uncheck it?
Try this ;)

<input name="myInput" id="myInput" maxLength="10" type="text" /> 
<input name="myCheckBox" id="myCheckBox" type="checkbox" onchange="javascript:changed(this);" />
<script>
  var txtField = document.getElementById('myInput');
  function changed(el){
    if(el.checked){
      alert('Alert content goes here...');
    }
    txtField.disabled = el.checked;
  }
</script>

Open in new window