Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I have a mouseover event fire for a child element if the parent element has a mouseover too?

I have a "help" area on my page and anytime the user hovers over a table the help information should be updated. The problem is within the table I have a check box in 1 cell of each row, and when the user hovers over that checkbox I want the mouseover event of the checkbox to override the table event and for the checkbox help to be displayed. Currently the table mouseover works fine but nothing happens when I mouse over the checkbox.

<table class="myTable">
   <tr>
      <th>Col1</th>
      <th>Col2</th>
   </tr>
   <tr>
      <td><input class="myCheckbox" type="checkbox" /></td>
      <td>Cell 2</td>
   </tr>
   <tr>
      <td><input class="myCheckbox" type="checkbox" /></td>
      <td>Cell 3</td>
   </tr>
</table>

<div class="myHelpMenu"></div>


$('.myCheckbox').mouseover(function() {
    $('.myHelpMenu').html("this is my checkbox help");
});

$('.myTable').mouseover(function() {
   $('.myHelpMenu').html("this is my tables help");
});
like image 709
silvster27 Avatar asked Mar 06 '13 23:03

silvster27


People also ask

Which event is triggered when a mouse is hovering over an element?

The onmouseover event occurs when the mouse pointer is moved onto an element, or onto one of its children. Tip: This event is often used together with the onmouseout event, which occurs when a user moves the mouse pointer out of an element.

What is the mouseover event fired?

The mouseover event is fired at an Element when a pointing device (such as a mouse or trackpad) is used to move the cursor onto the element or one of its child elements.

What is the difference between mouseover and Mouseenter?

The mouseover event triggers when the mouse pointer enters the div element, and its child elements. The mouseenter event is only triggered when the mouse pointer enters the div element. The onmousemove event triggers every time the mouse pointer is moved over the div element.

Is triggered when the mouse moves over an element?

mouseover: The onmouseover event triggers when the mouse pointer enters an element or any one of its child elements. mouseenter: The onmouseenter event is triggered only when the mouse pointer hits the element.


1 Answers

LIVE DEMO

This is a nice way to detect using mouseover the current target element that is hovered, than using pure JS to retrieve the .tagName you can create a messages list object and retrieve the desired one.

$('.myTable').mouseover(function( e ) {

  var tag = e.target.tagName;

  var messages = {
    "INPUT" : "this is my checkbox help",
    "TABLE" : "this is my tables help"
  };

  $('.myHelpMenu').text( messages[tag] );

});

If you want to clear your info message do like:

$('.myTable').on('mouseover mouseleave',function( e ) {

  var tag = e.target.tagName;

  var messages = {
    "INPUT" : "this is my checkbox help",
    "TABLE" : "this is my tables help"
  };

  $('.myHelpMenu').text( messages[tag] );

  if(e.type=='mouseleave')
    $('.myHelpMenu').empty();

});
like image 145
Roko C. Buljan Avatar answered Sep 24 '22 01:09

Roko C. Buljan