I want to call several id's in the javascript code so I don't have to create a different code for each Show More button:
<p><a href="javascript:mostrar();">Mostrar</a></p><div id="flotante"style="display:none;"><div id="close"><a href="javascript:cerrar();">cerrar</a></div> --CONTENIDO--</div>
I've already tried but it doesn't work, I've also tried ByClassName but it doesn't work either, does anyone know how to do it? javascript code
function mostrar() {
div = document.getElementById('flotante');
div.style.display = '';
}
function cerrar() {
div = document.getElementById('flotante');
div.style.display = 'none';
}
Usually, when one searches for an element by its id, it is usually unique, if not, using
class
instead ofid
becomes more logical, however, if multiple elements need to be selected, it can be done usingdocument.querySelectorAll
, for exampleClarification: ignore the css of the snippet that is purely from the example
However, as I mentioned at the beginning of the answer, if there is no particular requirement that prevents you from doing it, the ideal would be to use
class
, since applying the same behavior to severalid
, would be the same, right?To do so, a snippet could be the following:
It's true, getElementById only returns 1 value. If you add class="float" to your divs, you could get the list with any of the options: