tags:

views:

58

answers:

3

I have just discovered live events in jQuery.

I am trying it out. I have written a little snippet to bind the mouse hovering over submit buttons in a form:

jQuery("form img.submit_btn").live('hover', function(){
    (
      function () {
        jQuery(this).css('cursor','pointer');
      },
      function () {
        jQuery(this).css('cursor','auto');
      }
    );
});

However, it has no effect. Have I missed something?

[Edit]

It has just been brought to my attention that this behavior can be trivially implemeted using CSS. Perhaps then, the example I gave above is too simple. However, the underlying question still is how to use the 'live' call to bind hover events (so I can do something more complicated -e.g. animation etc, which cannot be done using CSS alone)

+1  A: 

You could do that with:

$('form img.submit_btn').mouseover(function() {
    jQuery(this).css('cursor','pointer');
});
$('form img.submit_btn').mouseout(function() {
    jQuery(this).css('cursor','auto');
});

OR

$('form img.submit_btn').mouseover(function() {
    jQuery(this).css('cursor','pointer');
  }).mouseout(function(){
    jQuery(this).css('cursor','auto');
});

Best of luck

Neurofluxation
+1  A: 

This seems a little redundant, just use CSS to define the hover state to use a pointer cursor rather than hand.

form img.submit_btn:hover {
   cursor: default;
}
Lazarus
You could even leave the `:hover` part out, since the cursor is only changed when it's over the element anyway.
RoToRa
To be fair, s/he did say that "I have just discovered live events in jQuery." So s/he is playing around and learning! Nothing wrong with that!!
Neurofluxation
@neurofluxation Good point, but IMHO part of learning JS/jQuery ist learning to know when **not** to use it.
RoToRa
@Lazarus: I get the point you're making (I must admit I hadn't thought of it - sometimes the simplest things are the best). Perhaps the example I gave could be trivially solved using CSS. This is not always the case though, so I still would like to know how to use the 'live' call to bind hover events (so I can do something more complicated)
morpheous
@RoToRa - Fair point. But at the same time. You know where I'm coming from. Agree to disagree.
Neurofluxation
@morpheous, I understand completely your intent here. One thing I would say is that live is intended for situations where you are creating new elements programmatically and want to have default events wired up, i.e. you have a grid and need to add edit-in-place functionality to each .editable element when a row is added through a button click. If the elements are static on the page then it's probably easier and more readable to use the direct events rather than hooking them up through live as I believe that's going to add a lot of 'plumbing' you simply don't need. Enjoy!
Lazarus
A: 

I believe .live() is more like .bind() than it is like the event methods. So, the problem here is twofold. First off, the above seems to expect the function passed as the event handler to return the two functions you list as a result of the function, but it won't work. You need a return keyword for that.

More importantly, if you pass hover to live, you have to basically do a forking event handler. From the jQuery example:

$('.hoverme').live('mouseover mouseout', function(event) {
  if (event.type == 'mouseover') {
    // do something on mouseover
  } else {
    // do something on mouseout
  }
});

jQuery 1.4.1 makes 'hover' map to 'mouseenter mouseleave', so you would do:

$('.hoverme').live('hover', function(event) {
  if (event.type == 'mouseenter') {
    $(this).css('cursor','pointer');
  } else {
    $(this).css('cursor','auto');
  }
});

That's for something more complicated, though. I agree with Lazarus that CSS is the way to go for just changing the pointer; I'm just assuming you wanted to do something more interesting and this was just an experiment.

Antonio Salazar Cardozo