Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
44
rated 0 times [  50] [ 6]  / answers: 1 / hits: 15941  / 11 Years ago, mon, june 10, 2013, 12:00:00

I have five text boxes and a submit button.



By default, my submit button is disabled.



I want that, on filling the values in all the text boxes, button should be enabled.



button should not enable after filling only some text boxes.


More From » javascript

 Answers
8

jQuery Solution



Demo



function doCheck(){
var allFilled = true;
$('input[type=text]').each(function(){
if($(this).val() == ''){
allFilled = false;
return false;
}
});
$('input[type=submit]').prop('disabled', !allFilled);
}

$(document).ready(function(){
$('input[type=text]').keyup(doCheck).focusout(doCheck);
});


Plain Javascript Solution



Demo



This requires the submit button have an ID of mysubmit:



function doCheck(){
var allFilled = true;

var inputs = document.getElementsByTagName('input');
for(var i=0; i<inputs.length; i++){
if(inputs[i].type == text && inputs[i].value == ''){
allFilled = false;
break;
}
}

document.getElementById(mysubmit).disabled = !allFilled;
}

window.onload = function(){
var inputs = document.getElementsByTagName('input');
for(var i=0; i<inputs.length; i++){
if(inputs[i].type == text){
inputs[i].onkeyup = doCheck;
inputs[i].onblur = doCheck;
}
}
};

[#77721] Friday, June 7, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
havenbilliec

Total Points: 324
Total Questions: 106
Total Answers: 94

Location: Pitcairn Islands
Member since Fri, Oct 15, 2021
3 Years ago
;