Reuse function in javascript

0

I have the following question, I want to manipulate a specific value in my function, where the assigned value is not fixed but dynamic (where I can change it as needed).

function mostrardiv() 
  {
      document.getElementById("teste").style.display = "block"; 
  }

In document.getElementById the id it receives is test but I wanted to have the freedom that when calling the function in code I could change the value that is assigned to document.getElementById.

I've looked into the subject, just found how to use the prototype in javascript, but I still do not understand how I can change the value assigned in document.getElementByI.

The way I thought it looked more like this:

function mostrardiv(val) 
  {
      document.getElementById(this.val).style.display = "block";
  }

But it did not work in html I used the function like this:

 <div class="row" >
      <div class="col-md-2" onmouseover="mostrardiv('teste')" > <img class="img-responsive" src="_imagens/1.png" >
 </div>
    
asked by anonymous 23.08.2017 / 22:07

1 answer

2

It would be more or less the same as below, but onmouseover="mostrardiv(this)" would have to be inside the element tag you want to get the ID.

<div class="col-md-2" onmouseover="mostrardiv(this)" > 
    <img class="img-responsive" src="_imagens/1.png" > 
</div>

function mostrardiv(campo) 
{
  document.getElementById(campo.value).style.display = "block"; 
}
    
23.08.2017 / 22:27