1
<script>
function clica(){
var x = document.getElementById("clica").value;
alert(x);
}
</script>
<?php
for ($i=0;$i<10; $i++){
?>
button onclick='clica()' id='clica' value='<?php echo $i;?>'><?php echo $i;?></button>
<?php
}
?>
I have this code and I would like every time you click a button to display its numerical value. But only the first value is displayed, and if I put the function inside the for, only the last one is displayed. How should I display the right value?
you are using the same id for all buttons, so it is not working. If you modify to
onclick="clica"
without the parentheses you can usevar x = this.value;
within the function– Pedro Sanção