JQUERY cakePHP – Enabling submit button if text entered

Tuesday, September 8, 2009 23:19

jQuery code snippet:

$('#username').keyup(function() {
    if ($('#username').val() != '') {
        $('#submit').removeAttr('disabled');
    } else {
        $('#submit').attr('disabled', 'disabled');
    }
});

It ensures that the submit button is only enabled if the “username” input field is not empty (you can see such a behavior on twitter for example).

However, the snippet from above can be rewritten in a shorter and more elegant way:

$('#username').keyup(function() {
    $('#submit').attr('disabled', !$('#username').val());
});
  • Share/Bookmark
You can leave a response, or trackback from your own site.

Leave a Reply