Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Henrik Christensen
Python Web Development Techdegree Student 38,322 PointsHelp with JS game
Hi :-)
I'm trying to make a JS game (very simple) to improve my JS skills but now I'm stucked cause I get an error (inputs.addEventListener is not a function) and I can't seem to fix it and I've been google'ing all day searching for an answer :-/
I'm trying to add a class (to select the users choice) to the input:radio that's being clicked on but can't make the addEventListener work.
I hope someone can help me :-)
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/style.css">
<script type="text/javascript" src="js/main.js"></script>
</head>
<body>
<div id="rps"></div>
</body>
</html
window.onload = function () {
//Creating all the elements on the page
var addElement = function(htmlTag, htmlText, htmlId, htmlType, htmlName, htmlFor) {
var myDiv = document.getElementById("rps");
var html = document.createElement(htmlTag);
var text = document.createTextNode(htmlText);
var id = html.setAttribute("id", htmlId);
if (htmlId === undefined || htmlId === false) {
html.removeAttribute("id");
}
var type = html.setAttribute("type", htmlType);
if (htmlType === undefined || htmlType === false) {
html.removeAttribute("type");
}
var name = html.setAttribute("name", htmlName);
if (htmlName === undefined || htmlName === false) {
html.removeAttribute("name");
}
var labelFor = html.setAttribute("for", htmlFor);
if (htmlFor === undefined || htmlFor === false) {
html.removeAttribute("for");
}
html.appendChild(text);
myDiv.appendChild(html);
}
addElement("h1", "Rock, Paper and Sciccors", "heading");
addElement("p", "In this game you will choose 'Rock', 'Paper' or 'Sciccors' and try to see if you can beat the computer.");
addElement("input", "Rock", "rock", "radio", "user_action");
addElement("label", "Rock", false, false, false, "rock");
addElement("input", "Paper", "paper", "radio", "user_action");
addElement("label", "Paper", false, false, false, "paper");
addElement("input", "Sciccors", "sciccors", "radio", "user_action");
addElement("label", "Sciccors", false, false, false, "sciccors");
addElement("button", "Rock, Paper, Sciccors", false, "button");
var rock = document.getElementById("rock");
rock.checked = true;
var paper = document.getElementById("paper");
paper.checked = false;
var sciccors = document.getElementById("sciccors");
sciccors.checked = false;
function addChecked() {
if (rock.checked === true) {
console.log("rock");
}
}
var inputTarget = document.getElementsByTagName("input");
inputTarget.addEventListener("click", addChecked);
};
2 Answers
Max Karacsony
23,928 Pointsnote that
document.getElementsByTagName("input")
returns a html collection of elements: https://developer.mozilla.org/de/docs/Web/API/document/getElementsByTagName
something like this should work
var inputTarget = document.getElementsByTagName("input")[0];
inputTarget.addEventListener("click", addChecked);
Moderator Edited: Moved from Comment section to Answer
Henrik Christensen
Python Web Development Techdegree Student 38,322 PointsCool, now I don't get that error anymore - it still need some adjustment but now I can continue :-) Thank you very much :-D