I am trying to copy the value of a hidden text field from another button. At the moment I'm trying to simply show it with an alert when I click on the button but it doesn't work. What am I doing wrong ?
php:
echo "<p id='campoCopiar' class='btn btn-warning btn-lg paginasTotales'>Páginas totales: <b>" . $paginasTotales . "</b></p>";
echo "<input id='oculto' name='oculto' type='hidden' value='" . $paginasTotales . "'>";
js:
document.querySelector("#campoCopiar").addEventListener("click", function(){
var oculto = document.querySelector("#oculto").value;
alert(oculto);
});
The variable is defined because one line above I have another code where it shows it and it works correctly.
The call to the js file I have it at the bottom just before the closing tag of the body
The $paginasTotales variable isn't declared anywhere in your code, it seems like it's empty. Maybe that's why?