Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep button disabled if text is not entered

Tags:

jquery

I want to achieve something like this. I have 4 text fields and one select field that are required for the form to submit. I want to keep the button disabled unless the text field has values and an option is selected from the dropdown. My code is here

The problem I have is whenever I enter the data in the 4 text fields and though the dropdown is selected to the default text the button gets enabled. After I change the dropdown it is disabled forever.

like image 667
user525146 Avatar asked Dec 08 '22 22:12

user525146


1 Answers

Try this - DEMO

$('#fname, #lname, #zipcode').on("keyup", action);
$('#location').on("change", action);

function action() {
    if( $('#fname').val().length > 0 && $('#lname').val().length > 0 && $('#zipcode').val().length > 0 && $('#location').val() != '' ) {
        $('#submit').prop("disabled", false);
    } else {
        $('#submit').prop("disabled", true);
    }   
}
like image 120
Zoltan Toth Avatar answered Jan 09 '23 17:01

Zoltan Toth