Reputation: 381
Below is my javascript code! I'm trying to increment the index of array $arr everytime the user clicks a button. The array is defined in a separate php tag! Where am I going wrong?
function option1() {
var i = 0;
document.getElementById("btn0").value = "newButtonValue";
document.getElementById("question").innerHTML =
"<?php echo $arr["results"][i++]["question"] ?>";
}
Upvotes: 1
Views: 1097
Reputation: 124
.html file
<button onclick="addIndex(this)" queNo="0">newButtonValue</button>
in .js file
function addIndex(btn) {
var i = btn.getAttribute("queNo")
console.log(i);
btn.setAttribute("queNo", i++);
document.getElementById("question").innerHTML = "<?php echo $arr['results']["+i+"]['question'] ?>";
}
Hope it's help you
Upvotes: 0
Reputation: 1062
Where your compiler return output to browser your php code was compiled and you can't run it such as javascript.
you can use this js:
var arr = <?php echo json_encode($arr["results"]);?>;
function option1() {
var i = 0;
document.getElementById("btn0").value = "newButtonValue";
document.getElementById("question").innerHTML = arr[i++]["question"];
}
Upvotes: 1