In my below work,I fetched serial 10
data,and show this data in button.
When I click button,the desired result is to show next choices.
But when I try this,$(".choice")
selector may not work well.
The following button is generated by js, is this cause of issue?
<button class='choice'>"+arr[i]+"</button>
If someone has any opinion, please let me know.
Thanks
// const fetch = require("node-fetch");
let clicked=0;
var apikey="https://opentdb.com/api.php?amount=10&type=multiple";
$(".start").on("click",function(){
fetch(apikey)
fetch(apikey)
.then(response => response.json())
.then(json => {
console.log(json);
display(json,0);
$(".choice").on("click",function() {
clicked++;
console.log("#");
console.log("clicked",clicked);
display(json,clicked);
});
});
});
function display(json,clicked){
const arr = [json.results[clicked].correct_answer].concat(json.results[clicked].incorrect_answers);
let html="";
for (let i=0;i<arr.length; i++){
html+= "<button class='choice'>"+arr[i]+"</button><br>";
}
document.querySelector('.btn').innerHTML = html;
}
<!DOCTYPE html>
<html>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script data-main="js/main.js" src="bower_components/requirejs/require.js"></script>
<div class="btn">
<button type="button" class="start">Start</button>
</div>
<script type="text/javascript" src="main.js"></script>
</html>