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.
4 Answers
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.
Comments
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
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>
selectelements...