0

I create element below dynamically

<ul class="services-list clearfix">
                    <li class="services-1" data-service-title="Hint 1"></li>
                    <li class="services-2" data-service-title="Hint 1"></li>
                    <li class="services-3" data-service-title="Hint 1"></li>
                    <li class="services-4" data-service-title="Hint 1"></li>
                    <li class="services-5" data-service-title="Hint 1"></li>
                    <li class="services-6" data-service-title="Hint 1"></li>
                </ul>

and then I need to add hover to this element.

I tried this code

$('.services-list > li').on({
    mouseenter: function () {
        var service_title = $(this).data( 'service-title' );
        $('<span class="service-hint"></span>').text(service_title).appendTo($(this));
    },
    mouseleave: function () {
        $('.services-list > li').find('.service-hint').remove();
    }
});

and this

$(document).on('hover', '.services-list > li', function () {
    var service_title = $(this).data( 'service-title' );
    $('<span class="service-hint"></span>').text(service_title).appendTo($(this));
}, function () {
    $('.services-list > li').find('.service-hint').remove();
});

but they don't work both. What's wrong? How to fix it?

5
  • did you include the jquery library? Commented Nov 18, 2014 at 13:44
  • of course! the first script works for static element, but doesn't work for dynamically generated element. Commented Nov 18, 2014 at 13:46
  • 1
    can you create a fiddle Commented Nov 18, 2014 at 13:47
  • @hronikata first script is working fine only , not for you ? Commented Nov 18, 2014 at 13:47
  • try to make a function in stead of $(document).on... but in your li use onhover="dofunction(this)" or something. Commented Nov 18, 2014 at 13:48

3 Answers 3

1

If your only concern is to show additional text on mouseover that can be available at the time these elements are added, I recommend using pure CSS to achieve the functionality:

HTML code

<ul class="services-list clearfix">
    <li class="services-1" data-service-title="Hint 1">Service 1 <span class="hint">Hint 1</span></li>
    <li class="services-2" data-service-title="Hint 1">Service 2 <span class="hint">Hint 2</span></li>
    <li class="services-3" data-service-title="Hint 1">Service 3 <span class="hint">Hint 3</span></li>
    <li class="services-4" data-service-title="Hint 1">Service 4 <span class="hint">Hint 4</span></li>
    <li class="services-5" data-service-title="Hint 1">Service 5 <span class="hint">Hint 5</span></li>
    <li class="services-6" data-service-title="Hint 1">Service 6 <span class="hint">Hint 6</span></li>
</ul>

CSS code

.services-list li span {
    display: none;
}

.services-list li:hover span {
    display: inline;
}

http://jsfiddle.net/jghr2cgv/


If that is not possible, you are on the right path with jQuery delegate events:

$('.services-list-container').on('mouseenter', '.services-list li', function () {
    // Mouse over $(this) element, even if the element was added later, but container was present initially
});

You must attach this type of event handling to an element that is present at the time the event is bound. I recommend a container for that task instead of document, as implied in the second part of my answer.

Sign up to request clarification or add additional context in comments.

Comments

1

The first area is just a normal .on() event, since you created in an object format, you can put the parent context / selector at the end. This will make it a delegated event.

$('.services-list > li').on({
    mouseenter: function () {
        /* code */
    },
    mouseleave: function () {
        /* code */
    }
}, $('body') ); // <-- now it's delegated to the parent context of body
            // use something more specific, maybe an ID of an <div> around your area

This should do the trick.

Also, as a side note:

Don't use document (or as I showed body above) as a parent context. That's one of the main reasons they removed .live() from the jQuery library. Make it much more specific, so the event doesn't bubble on EVERY mouseover on your page. Make it soething more specific! #parentDiv or something.

3 Comments

this script doesn't work for static and dynamically generated elements both :(
What version of jQuery are you using?
Sorry, look at my edit now. That will do the trick! @hronikata I had to wrap it in $('body') and it worked. Strange, but it worked in jsfiddle
0

You should be trying :

$(document).on('mouseover', '.services-list > li', function () {
var service_title = $(this).data( 'service-title' );
$('<span class="service-hint"></span>').text(service_title).appendTo($(this));
 }, function () {
$('.services-list > li').find('.service-hint').remove();
});

DEMO FIDDLE

Comments

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.