jquery - If input field is empty, disable submit button -


i'm trying disable submit button if user hasn't provided text.

at first sight looks works fine, if user types text, deletes it, submit button becomes enabled.

here code:

$(document).ready(function(){     $('.sendbutton').attr('disabled',true);     $('#message').keyup(function(){         if($(this).val.length !=0){             $('.sendbutton').attr('disabled', false);         }     }) }); 

you disabling on document.ready , happens once when dom ready need disable in keyup event when textbox gets empty. change $(this).val.length $(this).val().length

$(document).ready(function(){     $('.sendbutton').attr('disabled',true);     $('#message').keyup(function(){         if($(this).val().length !=0)             $('.sendbutton').attr('disabled', false);                     else             $('.sendbutton').attr('disabled',true);     }) }); 

or can use conditional operator instead of if statement. use prop instead of attr attribute not recommended jquery 1.6 , above disabled, checked etc.

as of jquery 1.6, .attr() method returns undefined attributes have not been set. retrieve , change dom properties such checked, selected, or disabled state of form elements, use .prop() method, jquery docs

$(document).ready(function(){     $('.sendbutton').prop('disabled',true);     $('#message').keyup(function(){         $('.sendbutton').prop('disabled', this.value == "" ? true : false);          }) });   

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -