Disable button 1 - what's wrong?


#1

I have tried everything. What's wrong here? When I try to save I 'm told:

"Oops, try again. Remember to add the line $('.btn').addClass('disabled'); outside your keyup event handler."

But it clearly is, isn't it?

var main = function() {
$('.btn').click(function() {
    var post = $('.status-box').val();
        $('<li>').text(post).prependTo('.posts');
        $('.status-box').val('');
        $('.counter').text('140');
    });

```javascript

$('.status-box').keyup(function() {
var postLength = $(this).val().length;
var charactersLeft = 140 - postLength;
$('.counter').text(charactersLeft);

if(charactersLeft < 0) {
$('.btn').addClass('disabled');
}
else if(charactersLeft == 140) {
$('.btn').addClass('disabled');
}
else {
$('.btn').removeClass('disabled');
}
});

$('.btn').addClass('disabled');
`

}

$(document).ready(main);


#2

The code below should work!

var main = function() {
$('.btn').click(function() {
var post = $('.status-box').val();
$('').text(post).prependTo('.posts');
$('.status-box').val('');
$('.counter').text("140");
});
$('.status-box').keyup(function() {
var postLength = $(this).val().length;
var charactersLeft = 140 - postLength;
$('.counter').text(charactersLeft);
if (charactersLeft < 0){
$('.btn').addClass('disabled');

}else if (charactersLeft <= 140){
$('.btn').removeClass('disabled');

}else{
$('btn').removeClass('disabled');
}

});
$('.btn').addClass('disabled');
}
$(document).ready(main);


#3

This worked for me !

Thanks :smile:


#4

Thanks, that worked!

I appreciate the help :smile:


#5

Hi,
This worked for me, but I have a question about why. One of my lines regarding 'else'
didn't have the correct code yet it worked. Here's mine:
}else{
$('btn').removeClass('disabled');
}

But I think it should've been: }else{
$('.btn').removeClass('disabled');
}

Can anyone help me to understand this?

Thanks.


#6

@julienk403,

Your assumption that the jQuery-Selector should be $('.btn') is correct.

The course-checker is only checking on specific items per section...
so you would get away with it....

But you would be confronted with your mistake during the run-phase....


#7

OK, thanks for your response.


#8

2 posts were split to a new topic: Checking charactersLeft - why doesn't this pass?