Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
78
rated 0 times [  83] [ 5]  / answers: 1 / hits: 95450  / 9 Years ago, tue, april 21, 2015, 12:00:00

How do I check if a textbox is disabled or enabled in JavaScript? I just want to know if a textbox is enable or disabled on button click. If the textbox is disabled I want to alert the user with the message disabled and if it is enable, the alert message will be enabled.



<script>
function CheckTextbox(){

if(textbox1 is disabled){ //here
alert(disabled);
}else{
alert(enable);
}

if(textbox2 is disabled){ //here
alert(disabled);
}else{
alert(enable);
}
}
</script>

<input type=text id=textbox1 disabled=disabled>
<input type=text id=textbox2>
<input type=button onclick=CheckTextbox();>

More From » javascript

 Answers
92

Try using document.getElementById('textbox1').disabled





function CheckTextbox() {
var id;
for (var i = 1; 2 >= i; i++) {
id = 'textbox' + i;
console.log(id, 'is disabled ?', document.getElementById(id).disabled);
}
}

<input type=text id=textbox1 disabled=disabled>
<input type=text id=textbox2>
<input type=button onclick=CheckTextbox(); value='Check'>




[#66980] Monday, April 20, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
parker

Total Points: 259
Total Questions: 109
Total Answers: 97

Location: Zambia
Member since Thu, Jun 25, 2020
4 Years ago
;