简体   繁体   中英

copy the value of a hidden field from a button

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM