12

I'm learning and using jQuery and want to display a delete icon for some elements.

I have an outer main div, which contains number of wrappers for elements. Inside the element wrapper, I want to display a delete icon when the user hovers over the element wrapper, and remove it when user moves out of the element wrapper.

Using mouseover and mouseout, I can display and remove the icon, but as soon as I move my mouse over the delete icon it is removed because it fires the mouseout event for the element wrapper. What am I doing wrong?

jcolebrand
  • 15,889
  • 12
  • 75
  • 121
KutePHP
  • 2,206
  • 6
  • 36
  • 54
  • In a "doh!" moment, I added the missing CSS answer to my answer. You didn't actually say you *had* to do this with JavaScript, did you? :-) But there's a JavaScript option as well (which you'll need if you want IE6 support). – T.J. Crowder Dec 16 '10 at 17:19

3 Answers3

12

Two options for you:

  1. CSS's :hover pseudo-class (but only if you don't have to support IE6)
  2. mouseenter and mouseleave events

CSS's :hover pseudo-class

You can make the browser do all the work if you don't need IE6 support, by using the :hover pseudo-class:

/* Don't show `child` elements inside `parent` elements...*/
parent child {
    display: none;
}

/* ...unless the `parent` element is being hovered over */
parent:hover child {
    display: block; /* or inline-block or whatever */
}

Live example

However, IE6 doesn't support the :hover pseudo-class except on a elements. IE7+ and all recent other browsers do.

mouseenter and mouseleave events

If CSS doesn't do it for you, you're looking for the mouseenter and mouseleave events, which are IE-specific but emulated by jQuery on all other browsers. jQuery even has a convenient function, hover, for hooking up handlers to both events so you can readily accomplish what you're looking to do.

$(...your parent element...).hover(
    function() {
        // Called when the mouse enters the element
    },
    function() {
        // Called when the mouse leaves the element
    }
 );

Here's a complete live example:

HTML:

<div>Hover over me <span class='del'>[X]</span></div>
<div>And me <span class='del'>[X]</span></div>
<div>And me <span class='del'>[X]</span></div>

JavaScript using jQuery:

$('div').hover(
  function() {
    $(this).find('span.del').show();
  },
  function() {
    $(this).find('span.del').hide();
  }
);

The reason you had trouble with mouseover and mouseout is that they bubble, and so your mouseout handler on your parent element was seeing the bubbled mouseout from the underlying element when your mouse moved into the delete element. mouseenter and mouseleave don't bubble, and so they don't have that problem.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Hey thanks, actually I had used this approach using jquery, but my in my case I want to append the icon on hover. With the above approach, when I move mouse over the icon multiple icons are added to the element wrapper. How to stop this ? – KutePHP Dec 17 '10 at 11:18
  • @KutaPHP: From your description, it sounds like you tried to use `mouseover` and `mouseout` rather than `mouseenter` and `mouseleave`. Again, they're different events and they behave differently (see note at the end of the answer above). But if you really were using `mouseenter` and `mouseleave`, it sounds like you weren't removing the elements on `mouseleave`. Works fine if you do: http://jsbin.com/umalu4/2 But adding to and removing from the DOM is a heavier-weight operation than showing/hiding, recommending showing/hiding. – T.J. Crowder Dec 17 '10 at 11:28
  • Hey thanks again...I fixed this...I was adding image directly, I added it as background to the span and I did it. :) – KutePHP Dec 17 '10 at 15:08
3

have you tried using mouseenter and mouseleave events instead?

mkoryak
  • 57,086
  • 61
  • 201
  • 257
0

You can apply style on the jQuery event onmouseover and onmouseout. When the user hovers over the menu this event will trigger there you can set effects.

<style type="text/css">
        .menu {
            background-color: #CDDC39;
            list-style: none;
            margin: 100px;
            padding: 0;
            width: 10em;
        }
            .menu li {
               margin: 0;
               padding: 5px;
           }
            .menu a {
            color: #333;
           }
    </style>
    <ul class="menu">
        <li onmouseover="this.style.backgroundColor='#F44336';" onmouseout="this.style.backgroundColor='transparent';" style="background-color: transparent;">
            <a href="http://www.infinetsoft.com/">learn dot net skills</a>
        </li>
        <li onmouseover="this.style.backgroundColor='#F44336';" onmouseout="this.style.backgroundColor='transparent';" style="background-color: transparent;">
            <a href="http://www.infinetsoft.com/htmltry">Work out html</a>
        </li>
        <li onmouseover="this.style.backgroundColor='#F44336';" onmouseout="this.style.backgroundColor='transparent';" style="background-color: transparent;">
            <a href="http://www.infinetsoft.com/Category/JQuery/9/1">jQuery tutorials</a>
        </li>
    </ul>

for more details

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mohamed Rasik
  • 148
  • 2
  • 8