0

Can you explain me how can I enable/disable a text box (that means, can or cannot be edit) depending on a combobox? If the combobox value = 1 -> textbox enable; if combobox = 0 -> textbox disable.

1
  • What have you tried so far? What combobox are you talking about? HTML only provides select elements... Commented May 30, 2011 at 11:59

4 Answers 4

3
var combo = document.getElementById('combo');
combo.onchange = function() {
    var value = document.getElementById('combo').value;
    var textBox = document.getElementById('textbox');
    if (value == '0') {
        textBox.setAttribute('readonly', 'readonly');
    } else if (value == '1') {
        textBox.removeAttribute('readonly');
    }    
};

and here's a live demo.

Sign up to request clarification or add additional context in comments.

Comments

1

You want to use the onchange event of the select element. Here's an example:

<html>
<head>
<script type="text/javascript">
function select1changed()
{
    var select1 = document.getElementById('select1');
    var txt1 = document.getElementById('txt1');
    txt1.disabled = (select1.value == '0');
}
</script>
</head>
<body>
    <form>
    Combo: 
    <select id="select1" onchange="select1changed();">
        <option value="0">Option 0</option>
        <option value="1">Option 1</option>
    </select>
    Text: <input id="txt1" type="text" value="sometext" disabled="disabled" />
    </form>
</body>

Note: I marked txt1 as disabled to handle the initial state when the page is loaded and select1 is set to Option 0.

Comments

0

Check this,

<select id='abc' onChange="changeMe()">
  <option value="">--Select--</option>
  <option value="111">111</option>
  <option value="222">222</option>
</select>
<input type="text" name="abcText" />
<script>
function changeMe(){
    if(document.getElementById("abc").value == "111"){
        document.getElementById("abcText").disabled = true;
    }else{
        document.getElementById("abcText").disabled = false;
    }
}
</script>

Comments

0

Yes, you can do it easily.

Make use of the disabled attribute of the text box like this

function enableField(val)
{
  document.form1.address2.disabled = val;
}

Note: address2 is a textfield

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.