-4

So, I have multiple div with the same class class="galao" and all of them have a p tag with the same class class="nomeGalao" as well. I'm trying to use a e.target in order to trigger a mouseover event and change the class of that especific p tag (div's child).

Here is my code

$(document).on({
  mouseenter: function() {
    e = e || window.event;
    var target = e.target || e.srcElement;
    $(target).children().addClass('invisible');
  },
  mouseleave: function() {
    e = e || window.event;
    var target = e.target || e.srcElement;
    $(target).children().addClass('invisible');
  }
}, ".galao");
.invisible {
  visibility: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="" class="galao">
  <div class="informacoesGalao">
    <div class="tabelaInformacoes infoGalao" style="float:right; text-align:right">
      <p class="invisible nomeGalao">Produto A</p>
    </div>
  </div>
</div>
<div id="" class="galao">
  <div class="informacoesGalao">
    <div class="tabelaInformacoes infoGalao" style="float:right; text-align:right">
      <p class="invisible nomeGalao">Produto B</p>
    </div>
  </div>
</div>

The problem is that I'm getting a undefined e, even when I'e already used a e.target in another function.

SAMUEL
  • 8,098
  • 3
  • 42
  • 42
Pedro Gonçalves
  • 119
  • 1
  • 1
  • 10

6 Answers6

3

e is expected to be the parameter of your event handler function. Notice that you don't need to do the e = e || window.event; var target = e.target || e.srcElement;, that's exactly the kind of normalisation that jQuery handles for you. Just use e.target directly:

$(document).on({
    mouseenter: function(e) {
        $(e.target).children().removeClass('invisible');
    },
    mouseleave: function(e) {
        $(e.target).children().addClass('invisible');
    }
}, ".galao");
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
2

Add parameters to your function :

function(e){}
Satpal
  • 132,252
  • 13
  • 159
  • 168
Lionel B
  • 1,172
  • 2
  • 9
  • 24
1

If i understand you correctly following will help you

You are missing parameter e in mouseenter and mouseleave function

$(document).on({
  mouseenter: function(e) {
    e = e || window.event;
    var target = e.target || e.srcElement;
    $(target).children().addClass('invisible');
  },
  mouseleave: function(e) {
    e = e || window.event;
    var target = e.target || e.srcElement;
    $(target).children().addClass('invisible');
  }
}, ".galao");
.invisible {
  visibility: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="" class="galao">
  <div class="informacoesGalao">
    <div class="tabelaInformacoes infoGalao" style="float:right; text-align:right">
      <p class="invisible nomeGalao">Produto A</p>
    </div>
  </div>
</div>
<div id="" class="galao">
  <div class="informacoesGalao">
    <div class="tabelaInformacoes infoGalao" style="float:right; text-align:right">
      <p class="invisible nomeGalao">Produto B</p>
    </div>
  </div>
</div>
SAMUEL
  • 8,098
  • 3
  • 42
  • 42
0
$(document).on({
     mouseenter: function (e) {
      var target = e.target || e.srcElement;
      ....
  },
     mouseleave: function (e) {
       ....
   }
  }, ".galao");
Raja Khoury
  • 3,015
  • 1
  • 20
  • 19
0

Replace what you have with this:

<script>
    $(document).on({
       mouseenter: function (e) {
          e = e || window.event;
          var target = e.target || e.srcElement;
          $(target).children().removeClass('invisible');
    },
       mouseleave: function (e) {
          e = e || window.event;
          var target = e.target || e.srcElement;
          $(target).children().addClass('invisible');
       }
    }, ".galao");
</script>

To explain, you need to pass 'e' as a parameter through your function, 'e' being the event. The links below may be helpful as well for your own personal reading:

http://javascript.info/tutorial/obtaining-event-object

https://www.w3schools.com/js/js_function_parameters.asp

jock.perkins
  • 469
  • 6
  • 23
0

You have to add a parameter to your function:

$(".nomeGalao").on("mouseenter", function(e){
      e = e || window.event;
      var target = e.target || e.srcElement;
      $(target).children().removeClass('invisible');
});

It is not going to work since you have no e object inside of your function. e stands for the event object, so please read the following: https://api.jquery.com/category/events/event-object/

margarita
  • 884
  • 1
  • 10
  • 21