0

I'm just trying to write some code like this:

var clickAction = function(){
    $('#OL_Icon_55').append("<em class='well'>Hello World</em>");
}
$("body").one("mouseenter",clickAction);

And I want to hide Hello World string when mouse hover around #OL_Icon_55, than the code will be look like this :

var clickAction = function(){
    $('#OL_Icon_55').append("<em class='well'>Hello World</em>", function() {
        $(this).hover(function () {
            $(".well").hide();
        });
    });
}
$("body").one("mouseenter",clickAction);

But that is not working for me. Does anyone have a little more idea or correct that code so that hover function can work?

4
  • The append method doesn't have a callback. Also FYI hover() is going to deprecated in the next jQuery. Commented Aug 8, 2012 at 21:18
  • can you give me some example so that code can work? Commented Aug 8, 2012 at 21:19
  • Why would you want to do this anyway? Why an event just once on body? Something seems wrong with your logic... Commented Aug 8, 2012 at 21:22
  • Yeah you might as well hardcode that html in.. Commented Aug 8, 2012 at 21:28

2 Answers 2

1

Have a look at this fiddle.

'Hello World' will be shown if the mouse is inside #OL_Icon_55.

Here's the JS:

var $OL = $('#OL_Icon_55'),
    $span = $("<span class='well'>Hello World</span>");

$("body").one("mouseenter", function() {
    $OL.append($span);
});

$OL.mouseenter(function () {
    $span.show();
}).mouseout(function () {
    $span.hide();
});                               
Sign up to request clarification or add additional context in comments.

Comments

1

Stole most the code from @depot. But much shorter

$(function(){  // <-- on dom ready
    var $OL = $('#OL_Icon_55'),
    $span = $("<span class='well'>Hello World</span>");  

    $("body").one("mouseenter", function() {  // <-- on one mouse enter
        $OL.append($span);  // <-- append span
    });

    $OL.hover(function () {  // <-- on hover
        $span.toggle();  // <-- toggle (show/hide) span
    });   
});       

http://jsfiddle.net/EeGYs/3/

1 Comment

liked your solution, since doesn't hide the div when you hover over it. +1

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.