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 trialHarshavardhan Gangavarapu
13,727 PointsNo expected output.
window.onload=function(){
let operation=document.querySelector("button");
function once(){
console.log("Done");
operation.removeEventListener("click",once());
}
}
<!DOCTYPE html>
<html>
<heaed><title>Harsha's blog</title>
<meta charset="utf-8">
<img src="https://lh3.googleusercontent.com/-43Fy-zIhnEo/XPpyAqkpvhI/AAAAAAAAADc/9mrAP_MPGO8h92lXfD4Ozzc3A1wbC-FdACEwYBhgL/w139-h140-p/IMG_20190607_193910585.jpg" class="photo">
<link rel="stylesheet" type="text/css" href="style.css">
<script src="index.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
</heaed>
<body>
<h1>G.H.Krishna Chaitanya</h1>
<p>G.H.Krishna Chaitanya is a boy who has knowledge about html,css, javascipt, python and java<br />
He has a hart of kindness. He is willing to help anyone who is in need and will never backdown<br />
If you want to contact him, see below for details
</p>
<a href="https://www.facebook.com/profile.php?id=100009945935184" target="_blank" class="facebook">Facebook</a>
<p class="contact">Contact number: 8328348702<br /></p>
<button type="button" id="button_j">JS</button><br />
<br/>
<button type="button" id="button_c">CSS</button><br />
<br />
<button type="button" id="button_h">HTML</button><br />
</body>
</html>
1 Answer
Steven Parker
231,248 PointsThere's no code here to associate an action with pressing a button. Perhaps you intended to add an event listener?
Also, when you add or remove a listener, you don't want to invoke the handler function, just reference it. So don't put ()'s after the function name.
So what you probably intended might be this:
window.onload = function() {
let operation = document.querySelector("button");
function once() {
console.log("Done");
operation.removeEventListener("click", once); // no ()'s
}
operation.addEventListener("click", once); // respond to the first click
};
Note that "querySelector" only selects the first item that meets the select criteria, so in this case it will be the "JS" button. If you wanted to add functionality to each button, you'd need to select them as a collection ("querySelectorAll"), and then use a loop to establish the handlers.
Harshavardhan Gangavarapu
13,727 PointsHarshavardhan Gangavarapu
13,727 PointsWhen i click on js or css or html buttons, nothing is printed in the console. Not even the error that is occurring. Please help me. Also, when i tried previously, one button functioned, while the other did not. Please help me out guys