Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enabled and disabled text input on checkbox checked and unchecked

Tags:

javascript

i've checkbox and text input

What I need is to enable text input when i check the checkbox and to disable text input when i uncheck the checkbox

I'm using the following code but it do the reverse enable when unchecked / disable when checked so how to adjust it fit with my needs.

<input type="checkbox" id="yourBox">
<input type="text" id="yourText">
<script>
document.getElementById('yourBox').onchange = function() {
document.getElementById('yourText').enabled = this.checked;
};
</script>

any help ~ thanks

like image 215
Reham Fahmy Avatar asked Feb 28 '13 16:02

Reham Fahmy


1 Answers

You just need to add a ! in front of this.checked.

Here's an example that shows the change:

document.getElementById('yourBox').onchange = function() {
    document.getElementById('yourText').disabled = !this.checked;
};
<input type="text" id="yourText" disabled />
<input type="checkbox" id="yourBox" />
like image 168
Minko Gechev Avatar answered Sep 19 '22 06:09

Minko Gechev