I use VS2010,C# for ASP.NET development, I’ve written a small Javascript function that enables an initially disabled button only after user inputs a phrase in a text box (this text box should not be empty), it works fine but there is a small problem, button is not enabled exactly when user types words, he should press tab or click on the screen so that button gets enabled! here is my JS function:
<script type="text/javascript" >
function onWeightChange() {
document.getElementById('btnFinish').disabled = true;
document.getElementById('btnConfirmQuestion').disabled = true;
if (document.getElementById('txtWeight').value != "") {
document.getElementById('btnFinish').disabled = false;
document.getElementById('btnConfirmQuestion').disabled = false;
}
}
</script>
and I call my JS in codebehind in the following way:
txtWeight.Attributes.Add("onchange", "return onWeightChange();");
what is going wrong here? I want my buttons to get enabled right after text is entered in textbox, and there should not be any need to change focus from textbox
thanks
onchangefires when the user leaves the element, it does not fire when the element has focus.You need to use a key event, either
onkeypressoronkeyupin combination with theonchangeevent. You need theonchangeevent since it is possible to change the value of the control without key presses!