0

In designing a chat app i have text area and button in a form. To disable send button for empty textarea i did below code:

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

Above code is disabling send button for initial blank space as well which i want to restrict (remember in textarea " hai" is fine but "(all blank spaces)" is not fine)

2
  • Try $('#btn1').prop('disabled', !this.value.trim()) Commented Sep 22, 2014 at 3:56
  • no @elclanrs its enabling send button always. Commented Sep 22, 2014 at 4:02

3 Answers 3

1
$('#msg').keyup(function(){
    var msg = this.value.trim(); // remove left and right blank spaces.
    $('#btn1').prop('disabled', msg == "" ? true : false);    
});

Just remove TOL and EOL blank spaces. If no trim method, implement it yourself.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

$('#msg').keyup(function(){
  var msg = $.trim($(this).val()); // remove left and right blank spaces.
  $('#btn1').prop('disabled', msg == "" ? true : false);    
});

Comments

1

Try this...

 $('#btn1').prop('disabled', true);
    $('#msg').keyup(function() {
        $('#btn1').prop('disabled', $.trim(this.value) === "" ? true : false);
    });

Comments

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.