3

I have a jsfiddle here.

jQuery

$(function() {
    //var input = $('form :input[type="text"]')
    $('form :input[type="text"]').live('keyup', function() {
        $('form .create-playlist-button')
            .attr('disabled', $('form :input[type="text"]').val().length == 0);
    });
});

Needed

  • When I start entering the data in the textbox, create should be enabled.

  • When I remove all text from the textbox, create should be disabled.

I am very new to jQuery and this thing is not working for me.

dda
  • 6,030
  • 2
  • 25
  • 34
daydreamer
  • 87,243
  • 191
  • 450
  • 722

5 Answers5

8
$('form :input[type="text"]').live('keyup', function() {
      var val = $.trim(this.value); // this.value is faster than $(this).val()
      $('form .create-playlist-button').prop('disabled', val.length == 0);
});

DEMO

Here, I used .prop() instead of .attr(), according to jQuery doc .prop() should be use. I also used .trim() for removing the whitespace from the beginning and end of value.


About your code

In your code you used $('form :input[type="text"]') two times, one for bind event and one to getting the value of text field. But that is not necessary, because within keyup event handler function this will refers to that input element on which keyup event binded.

If you sometime need to use any selector multiple times for any purpose it will be better to cache that in a variable and reuse that variable. Just for example:

var input = $('form :input[type="text"]');
input.on('click', function() {
      alert( this.value );
      input.addClass('something');
});

If would be better if you use .on() instead of .live() for delegate event handler, because .live() has been deprecated.

You can use .on() like following:

$('form').on('keyup', ':input[type="text"]', function() {
   var val = $.trim(this.value);
   $('form .create-playlist-button').prop('disabled', val.length == 0);
});

Note

Syntax of .on() for delegate event handling is like:

$(staticContainer).on( eventName, target, handlerFunction )

Here, staticContainer point to an element which belong to DOM at page load, i.e which is not dynamic and also container of target on which you want to bind your event.


Just for some more go here

Community
  • 1
  • 1
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
  • Huge thanks for the detailed answer, definitely helped me in understanding jQuery a little better, I appreciate your time @thecodeparadox – daydreamer Jul 01 '12 at 17:22
4

Updated:

$(function(){
    //var input = $('form :input[type="text"]')
    $('form :input[type="text"]').live('keyup',function(){   
            $(this).closest('form').find('.create-playlist-button').prop('disabled',$(this).val().length==0);
     });
});

In the "keyup" handler, you use this (or $(this) to use it via jQuery) to get at the text field that's actually involved. I also changed the code to ensure you'll find the correct "companion" button. It looks up the chain of parent elements to find the enclosing <form>, then finds the button inside that form.

The way you're assigning the event handlers is deprecated. It should be:

    $('form').on('keyup', ':input[type="text"]', function(){ ...

Also if you checked for "keypress" instead of "keyup" you'd fix the bug wherein the button doesn't work until the second character.

edit — oh and one more thing: you should usually use .prop() to update attributes, not .attr(). It's a confusing thing about the newer jQuery API, but the times you need to use .attr are kind-of rare. Unfortunately, there's a ton of old instructional material out there that was written back before jQuery 1.6 was released.

Pointy
  • 405,095
  • 59
  • 585
  • 614
0

You should use the .change() method !

And inside it juste do a test:

if ($(this).val().length > 0)
...
else
...
Maresh
  • 4,644
  • 25
  • 30
0

here ya go... using your original fiddle:

http://jsfiddle.net/9kKXX/4/

Trey
  • 5,480
  • 4
  • 23
  • 30
0

Only a small change is necessary.

$(function(){
//var input = $('form :input[type="text"]')
$('form :input[type="text"]').live('keyup',function(){   
        $('form .create-playlist-button').attr('disabled',$(this).val().length==0);
 });
 });

Fiddle: http://jsfiddle.net/9kKXX/36/

Cirrus xxx
  • 11
  • 3