javascript - jQuery and textarea change events - Stack Overflow

i just met a strange behavior jQuery and can't figure out any more or less slight solution.Say, i

i just met a strange behavior jQuery and can't figure out any more or less slight solution. Say, i have textarea and button. I want to disable button if textarea is empty. For this i have a handler which does this job.

Here is the code:

// textarea control
var $textarea = $('#myTextarea');
var $button = $('#myButton');
$textarea.on('input propertychange', function() {
    if($textarea.val().length > 0) {
        $button.removeClass('disabled').removeAttr('disabled');
    } else {
        $button.addClass('disabled').attr('disabled', 'disabled');
    }
});
$button.on('click', function() {
    if ($button.attr("disabled") != null) {
        console.log('Disabled!');
        return false;
    } else {
        // do some stuff and eventually erase textarea 
        $textarea.val('');
    }
});

My trouble is when i erase textarea (the end of the code) it doesn't disable the button. Any ideas would be appreciated (actually it's slight adaptation of my code but the situation reflected pretty good, hope it would be clear for you, thanks!)

UPD Nothing found on stackoverflow doesn't help.

UPD2 As i said in the begin, i was looking not for workaround like force trigger event, i thought it's possible to catch any event fired by $textarea.val(); Sure @Madbreaks and @epascarello 's solutions work pretty good, thanks guys.

i just met a strange behavior jQuery and can't figure out any more or less slight solution. Say, i have textarea and button. I want to disable button if textarea is empty. For this i have a handler which does this job.

Here is the code:

// textarea control
var $textarea = $('#myTextarea');
var $button = $('#myButton');
$textarea.on('input propertychange', function() {
    if($textarea.val().length > 0) {
        $button.removeClass('disabled').removeAttr('disabled');
    } else {
        $button.addClass('disabled').attr('disabled', 'disabled');
    }
});
$button.on('click', function() {
    if ($button.attr("disabled") != null) {
        console.log('Disabled!');
        return false;
    } else {
        // do some stuff and eventually erase textarea 
        $textarea.val('');
    }
});

My trouble is when i erase textarea (the end of the code) it doesn't disable the button. Any ideas would be appreciated (actually it's slight adaptation of my code but the situation reflected pretty good, hope it would be clear for you, thanks!)

UPD Nothing found on stackoverflow doesn't help.

UPD2 As i said in the begin, i was looking not for workaround like force trigger event, i thought it's possible to catch any event fired by $textarea.val(); Sure @Madbreaks and @epascarello 's solutions work pretty good, thanks guys.

Share Improve this question edited Jan 16, 2013 at 22:25 tony asked Jan 16, 2013 at 22:13 tonytony 1,5463 gold badges21 silver badges28 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

Maybe just add this:

else {
    // do some stuff and eventually erase textarea 
    $textarea.val('');
    // notify
    $textarea.trigger('propertychange');
}

problem is setting values with JavaScript does not normally trigger the events, but you can do it yourself.

$textarea.val('').trigger("input");

Make sure you trim the value of your textarea and you dont have an white spaces when you get the length of the .val(). Also in case you use a text editor like ckEditor make sure you read their documentation on how to retrieve the text being written.

Remend using this jQueryTextChange lib. Its a cross browser consistent one. The usages are clearly elaborated on the home page. http://zurb./playground/jquery-text-change-custom-event

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745111007a4611850.html

相关推荐

  • javascript - jQuery and textarea change events - Stack Overflow

    i just met a strange behavior jQuery and can't figure out any more or less slight solution.Say, i

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信