<!DOCTYPE html>
<html>
<head>
</head>
<button id="someid" onclick="open()">Open A Booster Pack</button>
<button id="somid" onclick="view()">View Garage</button>
<body>
<p id="demo"></p>
</body>
<script>
function open(){
itms = ['2014 Nissan 350z', '2019 VW golf GTI ', '1995 Mazda 323 Protoge', '1990 BMW 8-series', '1990 ford mustang svt cobra', '1990 mazda rx7', '1990 nissan skyline gt-r'];
itm = itms[Math.floor(Math.random()*itms.length)];
console.log(itm)
}
function view(){
document.getElementById("demo").innerHTML = ""+itm+""
}
</script>
</html>
I have no idea why all the elements dissapear, even the DOCTYPE declaration vanishes when either button is clicked, for those who want to know I am trying to make a car collection card game website.
2
Answers
open()
is already the name of a function that opens a new page, so when the button is clickedopen()
is called and an empty page is opened. Just change your function name:Also, consider changing your code to use event listeners and variables instead. It is better practice:
Try to change the name of your function. I’m not sure exactly where this code leads, but here’s the fix:
btw, try to avoid innerHtml due to the potential risk it has. Try using innerText or innerContent. Hope it helps