0

I want to make a checkbox to control two textfield

The checkbox default checked

and two textfield default disabled

When user uncheck the checkbox , These two textfield do not disabled.

How can I write the script?

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by fobos
0

Here is something that i came up with, but its untested and its just to get you going. I would suggest using jQuery because the code would be much more simple with this problem that you are having.

<script type="text/javascript">
function toggle(){
    if (document.getElementById("input1").disabled == false){
        document.getElementById("input1").disabled = true;
        document.getElementById("input2").disabled = true;
        return
    }
    if (document.getElementById("input1").disabled == true){
        document.getElementById("input1").disabled = false;
        document.getElementById("input2").disabled = false;
        return
    }

} 
</script>

Then just call it using the jquery onload function.

HTML

<input type="checkbox" id="check1" onclick="toggle()" />
<input type="text" id="input1">
<input type="text" id="input2">

hope this helps

Edited by Dani: Formatting fixed

0

Man my javascript never posted.

function toggle() {
    if(document.getElementById("check1").checked){
        document.getElementById("input1").disabled = true;
        document.getElementById("input2").disabled = true;
    } else {
        document.getElementById("input1").disabled = false;
        document.getElementById("input2").disabled = false;
    }
}

Edited by Dani: Formatting fixed

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.