-1

I have elements like so:

<div class="parent">
  <div class="one"></div>
  <div class="two"></div>
  <div class="child"></div>
  <div class="child"></div>
  <div class="three">
    <div class="four">
      <div class="child"></div>
      <div class="five"></div>
    </div>
  </div>
</div>

Is it possible to apply 'mousemove' event, to ".parent", which won't be triggered over ".child" elements which may not be direct descendants?

I tried combinations of .children, .not, but nothing seems to work.

Edit: Thanks for suggested duplicate question and downvote, but I can't see how it applies to my question.

zee
  • 359
  • 3
  • 16
  • Possible duplicate of [How to ONLY trigger parent click event when a child is clicked](https://stackoverflow.com/questions/38861601/how-to-only-trigger-parent-click-event-when-a-child-is-clicked) – Sphinx Apr 11 '18 at 19:24
  • This is not a duplicate of that question. OP is asking for trigger click only in the parent. Though an answer could be found in that question. – Andre Figueiredo Apr 11 '18 at 19:34
  • Possible duplicate of [How to have click event ONLY fire on parent DIV, not children?](https://stackoverflow.com/questions/9183381/how-to-have-click-event-only-fire-on-parent-div-not-children) – Andre Figueiredo Apr 11 '18 at 19:36

1 Answers1

0

Is this what you mean? this code has the event attached to the parent, but dependent on what element is hovered, it will act differently. In this case, if it has the class child.

    $(".parent").on('mousemove', function(e){
         console.log(e);
         if(e.target.className === 'child'){
             console.log('do nothing, this has child class')
         } else {
             console.log('do whatever, this does not have the child class')
         }
    });

here is a jsfiddle as well jsfiddle

Truextacy
  • 562
  • 1
  • 5
  • 26
  • Yes, this does the job, thank you. I was concerned about checking condition on 'mousemove' and was trying to find a solution with selectors, but this actually works fine – zee Apr 12 '18 at 17:36
  • Awesome, notice I am checking className in this case, with multiple classes you may have to check the `classList` of the target object, and iterate through them to get the desired behavior. Glad I could help. – Truextacy Apr 12 '18 at 17:37