2

I need to add a jquery listener to some dynamically created elements. I cannot for the life of me to get this to work. tag_options is not dynamic, its children are. I have tried three ways:

http://jsfiddle.net/nfe2f/

<div id = "tag_options">
    <div class = "tag_option">hover me</div>
</div>

The js:

// never works
$('#tag_options').delegate('.tag_option', 'hover', function(event){
 alert();
});


// never works
$("#tag_options").on("hover", "[class='tag_option']", function(event) {   
    alert();
});


// works if not dynamically created
$('#tag_options .tag_option').hover( function(){
    alert();
 });
17
  • 3
    @milkshake why is tag_options not a valid class/id name? Commented Aug 7, 2013 at 14:25
  • 1
    @milkshake really? Commented Aug 7, 2013 at 14:26
  • 1
    News to me too: developer.mozilla.org/en/docs/Underscores_in_class_and_ID_Names Commented Aug 7, 2013 at 14:29
  • 1
    That seems to be something published 12 years ago which is probably irrelevant now. Commented Aug 7, 2013 at 14:31
  • 1
    @Ian Fair enough, hard habit to shake though, plus it will not hurt my code :D Commented Aug 7, 2013 at 14:44

5 Answers 5

2

There is no hover event anymore and delegate is being phased out in favor of on. You'd want to use mouseenter and mouseleave. I believe you're shooting for this:

$('#tag_options')
  .on('mouseenter', '.tag_option', function(e){
    /* Wax On */
  })
  .on('mouseleave', '.tag_option', function(e){
    /* Wax Off */
  });

If you wish to keep it in one on you can do what palaѕн suggests. I just wanted to make it a little easier to follow:

$('#tag_options').on('mouseenter mouseleave', '.tag_option', function(event){
  if ( event.type === 'mouseenter' ) {
    /* Wax On */
  } else {
    /* Wax Off */
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, that did it. Tricky that .hover() works in some cases.
2

This happens since there is no .on("hover" event. You can use mouseenter or mouseleave like:

$(function () {

    // Attach Event
    // new way (jQuery 1.7+) - on(events, selector, handler);
    $('#tag_options').on('mouseenter mouseleave', '.tag_option', function (e) {
        alert(e.type);
    });

    // Create elements dynamically
    $('#tag_options').append('<div class="tag_option">hover me</div>');
});

FIDDLE

Comments

0

Other syntax possible, more readable IMO:

$('#tag_options').on({
    mouseenter:function(){/**/},
    mouseleave:function(){/**/}
},'.tag_option');

Comments

0

example to go with first answer:

$('#tag_options').on('mouseenter mouseleave','.tag_option', function(event){
    if(event.type == 'mouseenter'){
        alert('entering');
    }
    if(event.type == 'mouseleave'){
        alert('leaving');
    }
});

3 Comments

If you want to post an example related to an already existing answer, do it as a comment or link to a JSFiddle.
@JeffNoel appologies.
Quick heads up: You forgot the .type on event.
-1

There is a mouseover event that you can listen for:

$('#tag_options').on("mouseover", ".tag_option", function(event) {   
    alert('hello world!');
});

jsFiddle

1 Comment

I wouldn't recommend using mouseover as it will fire for child elements as well, see the demo at the bottom of api.jquery.com/mouseover

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.