user2699508
user2699508

Reputation: 63

jQuery script for checking if input has any value. Issue in IE and Chrome

I've got the script for checking if an input isn't empty. It works in Firefox, but if I switch to Chrome or IE, button is enabled even if I submit an empty string. Does anybody know any solution for this issue?

 $(document).ready(function(){

 $('.comment_button').attr('disabled',true);

 $('.comment_input').keyup(function(){
    if ($.trim($(this).val()).length != 0)  {
        $('.comment_button').attr('disabled', false);
    }
    else
    {
        $('.comment_button').attr('disabled', true);        
    }
 })
 });

Upvotes: 0

Views: 408

Answers (1)

Satpal
Satpal

Reputation: 133433

There is a typo in your code ' is missing after selector

Use $('.comment_input') instead of $('.comment_input)

In line

$('.comment_input).keyup(function(){

Edit: use .prop() instead of .attr()

Working code JS:

 $('.comment_button').prop('disabled', true);
 $('.comment_input').keyup(function () {
      $('.comment_button ').prop('disabled', $.trim($(this).val()).length == 0);
 });

HTML:

<input type="text" class="comment_input" name="somethng"/>
<button class="comment_button">something</button>

Fiddle Demo

Upvotes: 4

Related Questions