Create element only on first mouseover

2

I'm making a website and have an image in it that activates a Javascript function through onmouseover using a function . One of these methods I used in Javascript adds a new tag in a span with an "X" id on which this image is, except that the method instead of only creating the tag, creates the tag each time the mouse passes over the image.

I need this method to only create the tag once when the mouse passes over the image.

Below the image html:

<span id="ex1" >
<img width="794px" height="593px" src="images/crateria.png" style="margin:0;" onmouseover="javascript:mostra();" alt="Crateria" />
</span>

Below the Javascript I used to add the tag, in this case the tag <map> :

function mostra(){
   var para = document.createElement("map");
   para.setAttribute("name", "crateria-map");
   document.getElementById("ex1").appendChild(para);
}
    
asked by anonymous 23.03.2015 / 03:39

3 answers

3

I suggest not creating it in onmouseover , but already have it in HTML - only hidden. So you can simply display / hide it with CSS without needing JavaScript:

#ex1 map {
  display:none;
}

#ex1 img:hover + map {
  display:block;
  border: 1px solid red; /* Só para ajudar a ver que funciona */
}
<span id="ex1" >
<img width="794px" height="593px" src="images/crateria.png" style="margin:0;" alt="Crateria" />
  <map name="crateria-map">
  </map>
</span>

If, on the other hand, you want the tag to remain there after the first mouse pass, one way to do this (not necessarily the best) is to reset the function used in onmouseover after it is first used:

function mostra(){
    var para = document.createElement("map");
    para.setAttribute("name", "crateria-map");
    document.getElementById("ex1").appendChild(para);

    mostra = function() { /* Não faz nada */ }
}
#ex1 map {
  border: 1px solid red; /* Só para ajudar a ver que funciona */
}
<span id="ex1" >
<img width="794px" height="593px" src="images/crateria.png" style="margin:0;" alt="Crateria" onmouseover="javascript:mostra();" />
</span>

Finally, if there are N images and not just one, a variation of the bfavaretto response should resolve your problem: check not a specific element (by id), but rather the parent of the image receiving hover (using event.target to get the image, parentElement to get span lastChild to get the last child element). Example:

function mostra(){
  if ( event.target.parentElement.lastChild.nodeName != "MAP" ) {
    var para = document.createElement("map");
    para.setAttribute("name", "crateria-map");
    event.target.parentElement.appendChild(para);
  }
}
map {
  border: 1px solid red; /* Só para ajudar a ver que funciona */
}
<span id="ex1" >
<img width="100px" height="100px" src="images/crateria.png" style="margin:0;" alt="Crateria" onmouseover="javascript:mostra();" />
</span>

<span id="ex2" >
<img width="100px" height="100px" src="images/crateria.png" style="margin:0;" alt="Crateria" onmouseover="javascript:mostra();" />
</span>

<span id="ex3" >
<img width="100px" height="100px" src="images/crateria.png" style="margin:0;" alt="Crateria" onmouseover="javascript:mostra();" />
</span>
    
23.03.2015 / 04:20
3

You can check that <span> already contains a <map> at the end, and only create and insert the map if it is not already there:

function mostra(){
    var span = document.getElementById("ex1");
    // verifica se o último filho do span é um map
    var contemMapa = span.lastChild.nodeName === 'MAP';
    if(!contemMapa) {
        var para = document.createElement("map");
        para.setAttribute("name", "crateria-map");
        document.getElementById("ex1").appendChild(para);
    }
}

link

    
23.03.2015 / 04:18
1

You could try putting a counter, checking if the function has already been run once and so it does not run anymore. Example below:

tag = 0;

function mostra(){
     if (tag != 1)
   {
   var para = document.createElement("map");
   para.setAttribute("name", "crateria-map");
   document.getElementById("ex1").appendChild(para);
   tag = 1;
   }

}
    
23.03.2015 / 04:26