Excellent day, after several days or weeks I asked my programming master how he could make that having several buttons execute the same script on a single sheet the same function of hiding divs and appearing the respective div to his button, It is easier than I thought, making a switch statement.
link
Basically it is the following.
switch(expression) {
case n:
code block
break;
case n:
code block
break;
default:
code block
}
So what I did was this:
function myFunction(idButton) {
var producto1 = document.getElementById('producto1');
var producto2 = document.getElementById('producto2');
var producto3 = document.getElementById('producto3');
switch(idButton) {
case 1:
producto1.style.display = 'block';
producto2.style.display = 'none';
producto3.style.display = 'none';
break;
case 2:
producto1.style.display = 'none';
producto2.style.display = 'block';
producto3.style.display = 'none';
break;
case 3:
producto1.style.display = 'none';
producto2.style.display = 'none';
producto3.style.display = 'block';
break;
default:
alert("hay un problema: No existe el producto.")
}
}
Where the number that contains "case" is the id that is assigned to the onclick button in html.
<li onclick="myFunction(1)">Producto1</li>
<li onclick="myFunction(2)">Producto2</li>
<li onclick="myFunction(3)">Producto3</li>
those up here would be the buttons and I would do it with li
<div id="producto1" style="display:none;">
<div class="tituloDiv">
<h3>Producto1</h3>
</div>
<div class="contenidoDiv">
<p>Descripción producto1.</p>
</div>
</div>
<div id="producto2" style="display:none;">
<div class="tituloDiv">
<h3>Producto1</h3>
</div>
<div class="contenidoDiv">
<p>Descripción producto2.</p>
</div>
</div>
<div id="producto3" style="display:none;">
<div class="tituloDiv">
<h3>Producto1</h3>
</div>
<div class="contenidoDiv">
<p>Descripción producto3.</p>
</div>
</div>
Greetings c: