1

I have a form with two inputs, submit button is disabled by default. I want to enable the button if the inputs are not empty or when the browser auto fill the form. Is there a way of enabling the button when browser auto fill the form?. Currently I'm using setTimeout.

function notEmpty(){
if ($('#password').val() !== '' && $('#useremail').val() !== ''){
    button.prop('disabled',false);
}
}
setTimeout(function(){ notEmpty(); }, 5000);

2 Answers 2

3

JS works better when you program it on event driven programming style.

You can use the trigger oninput to all the elements that you want to watch out for

$(function(){
    $('input').on('input',function(){
        if ($('#password').val() !== '' && $('#useremail').val() !== ''){
             $('button').prop('disabled',false);
        }
    }).trigger('input');
});
Sign up to request clarification or add additional context in comments.

2 Comments

Works, but I have to click on the page before it is enabled.
You can trigger input programatically on document.ready() use $('input').trigger('input')
1
$('#password, #useremail').change( function() {
    if ( $('#password').val()!="" && $('#useremail').val()!="" )
        button.prop('disabled',false);
});

3 Comments

Can you also explain the OP why and how your solution works?
well, jquery basically uses css syntax when it comes to selectors, so the most efficient way (i believe) is to bind the "change" event to both elements in the same declaration. the rest is pretty straightforward
true, but why did't you add this to your answer? :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.