The Code
- <html>
-
<head>
-
<title>Enable/Disable Submit Button based on Multiple Textbox values</title>
-
</head>
-
<body>
-
<p>Enter some values in the text to Enable the Submit button!</p>
-
-
<p>Name: <input type="text" id="name" placeholder="Enter your name"
-
onchange="manage(this)" /></p>
-
Designation: <input type="text" id="desig" placeholder="Enter designation"
-
onchange="manage(this)" />
-
-
<input type="submit" id="submit" disabled />
-
</body>
-
-
<script>
-
function manage(txt) {
-
var bt = document.getElementById('submit');
-
var ele = document.getElementsByTagName('input');
-
-
// LOOP THROUGH EACH ELEMENT.
-
for (i = 0; i < ele.length; i++) {
-
-
// CHECK THE ELEMENT TYPE.
-
if (ele[i].type == 'text' && ele[i].value == '') {
-
bt.disabled = true; // Disable the button.
-
return false;
-
}
-
else {
-
bt.disabled = false; // Enable the button.
-
}
-
}
-
}
-
</script>
-
</html>
ok - since 'the code' is constantly posted regardless of being not useful in this context (since its not related to the OP's requirements) lets use it as an example of how not to write Javascript code (even for the 'selfchosen' example the poster decided to throw out code for):
1. function has a meaningless name
2. function gets a parameter passed that is never used
3. the variable i is an implicit global
4. wrong inline comment
5. return value not used but return misused to break out of the loop
6. loop is unnecessarily evaluating length in each iteration
7. element lookup in loop happens twice instead of 1 time only per iteration