jquery - .addClass('disabled') doesn't work -
i having trouble disabling button. trying disable button unless typing words in input. following code.
$('.btn.btn-default').click(function(){ var post = $('input:text').val(); $('<li>').text(post).prependto('.posts'); $('input:text').val(""); $('.count').text('100'); $('.btn.btn-default').addclass('disabled'); }); $('input:text').keyup(function(){ var postlength = (this).val().length; var charactersleft = 100 - postlength; $('.count').text(charactersleft); if (charactersleft < 0){ $('.btn.btn-default').addclass('disabled'); } else if (charactersleft == 100){ $('.btn.btn-default').addclass('disabled'); } else{ $('.btn.btn-default').removeclass('disabled'); } }); $('.btn.btn-default').addclass('disabled'); };
the following html
<div class= "blank"> <input type="text" id="form-control" placeholder="please leave comment here."/> </div> <div class = "status"> <div class= "count">100</div> <input class="btn btn-default" type="submit" value="post"> </div> <ul class="posts"> </ul>
typo:
(this).val().length; ^^^
add missing $
. should error in console pointing out "val not function".
also adding class not disable button. need set property.
$(".btn.btn-default").prop('disabled', true); $(".btn.btn-default").prop('disabled', false);
Comments
Post a Comment