0

Possible Duplicate:
jQuery 1.7 - Turning live() into on()

I have this code:

<script>
        jQuery('#ultimecomunicazioni')
    .live("mouseenter", function() {
        jQuery(this).append('<span id="ultimecomunicazioni_appear" style="font-weight:normal;margin-left:5px">(<a style="color:grey" href="<?php echo esc_url( home_url( '/' ) ); ?>comunicazioni/">tutte</a>)</span>');
    })
    .live("mouseleave", function() {
        jQuery(this).children('#ultimecomunicazioni_appear').remove();
    });
</script>

I'd like to change the two .live to .on and combine the 2 handlers in one. I tried to use this example by TJ but I get confused from the 'tr' at the end.. it should be something like this but I'm not sure:

<script>
jQuery('#ultimecomunicazioni').on({
    'mouseenter' : function () {
        jQuery(this).append('<span id="ultimecomunicazioni_appear" style="font-weight:normal;margin-left:5px">(<a style="color:grey" href="<?php echo esc_url( home_url( '/' ) ); ?>comunicazioni/">tutte</a>)</span>');
     },
    'mouseleave' : function () {
        jQuery(this).children('#ultimecomunicazioni_appear').remove();
    }
});
</script>
Community
  • 1
  • 1
MultiformeIngegno
  • 6,545
  • 14
  • 54
  • 107

6 Answers6

1

Just read the docs for .live and the part on delegated events for .on, it's explained there pretty good.

Rewriting the .live() method in terms of its successors is straightforward; these are templates for equivalent calls for all three event attachment methods:

$(selector).live(events, data, handler); // jQuery 1.3+

$(document).delegate(selector, events, data, handler); // jQuery 1.4.3+

$(document).on(events, selector, data, handler); // jQuery 1.7+

However, I can't see why you ever would need delegated functionality on one element with a certain id. It seems likely that you just experience this issue.

Community
  • 1
  • 1
Bergi
  • 513,640
  • 108
  • 821
  • 1,164
1

Try this:

$(document).on("mouseenter mouseleave", "#ultimecomunicazioni" , function(e) {
    if(e.type === 'mouseenter'){
        $(this).append('<span id="ultimecomunicazioni_appear" style="font-weight:normal;margin-left:5px">(<a style="color:grey" href="comunicazioni/">tutte</a>)</span>');
    } else {
        $('#ultimecomunicazioni_appear').remove();
    }
});

Note: I have removed the little php echo code since that does not work with javascript (server vs client side languages)

Gustavo Hoirisch
  • 1,580
  • 10
  • 18
0

You Can try this :

<script>
jQuery('#ultimecomunicazioni').parent().on({
    'mouseenter' : function () {
        jQuery(this).append('<span id="ultimecomunicazioni_appear" style="font-weight:normal;margin-left:5px">(<a style="color:grey" href="<?php echo esc_url( home_url( '/' ) ); ?>comunicazioni/">tutte</a>)</span>');
     },
    'mouseleave' : function () {
        jQuery(this).children('#ultimecomunicazioni_appear').remove();
    }
},
'#ultimecomunicazioni'
);
</script>
Ahmed Assaf
  • 571
  • 5
  • 23
0

You're forgetting about the fact, that jQuerys original .live() method was in that way special, that it always bound the event listener to the document.body. That was big critique point also, because most of the time it was unnecessary to place the handler so far away at the top of the document.

.delegate() was more like .on() in its current form, but they changed the order of arguments from

method(<element>, <event>, <callback>)

into

method(<event>, <element>, >callback>)

I'm uncertain if that affects your original code, but if it requires for some reason that the event handler is on document.body level instead the one you called .on() for, it might cause some trouble.

jAndy
  • 212,463
  • 51
  • 293
  • 348
  • Uhm.. my just made an element appear when I passed the mouse on another element. you can see it live here http://goo.gl/8Nxnu (search for "Ultime circolari", when you hover it an element appears at its right). – MultiformeIngegno Feb 03 '13 at 13:50
0

As i saw your code this seems that you are having issues with the php part in the appending content:

jQuery('#ultimecomunicazioni').on({
'mouseover': function () {
    jQuery(this).append('<span id="ultimecomunicazioni_appear" style="font-weight:normal;margin-left:5px">(<a style="color:grey" 
href="<?php echo esc_url( home_url( / ) ); ?>comunicazioni/">tutte</a>)</span>');
},    //---------------------------^-^-----------------------here
    'mouseleave': function () {
    jQuery(this).children('#ultimecomunicazioni_appear').remove();
}
});

i just removed the quotes here and your code worked : http://jsfiddle.net/CrXZr/

Jai
  • 71,335
  • 12
  • 70
  • 93
  • Uhm.. really weird! Anyway in the fiddle everytime I pass the mouse on the "(tutte)" another element appears – MultiformeIngegno Feb 03 '13 at 14:02
  • yes because div is not fixed with width and height and you can use `event.stopPropagation()` on span's mouse over. – Jai Feb 03 '13 at 14:24
0
$(document.body).on({
    'mouseenter' : function () {
        $('<span id="ultimecomunicazioni_appear"/>').appendTo($(this));
 },
    'mouseleave' : function () {
       $(this).find('#ultimecomunicazioni_appear').remove();
 }, '#ultimecomunicazioni'

});

mikakun
  • 2,105
  • 2
  • 14
  • 24
  • Uhm.. see your example live here http://goo.gl/8Nxnu (search for "Ultime comunicazioni", at the right should appear an element, like for "Ultime circolari", the widget just below, but nothing happens..) – MultiformeIngegno Feb 03 '13 at 14:06
  • if nothing happens, comes from css or html (typing error, no definition missing...) – mikakun Feb 03 '13 at 14:15