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

JavaScript

why button is not working

hey i m just extending my quiz code but i m not able to call function "begin" through button.... please check out my code here http://codepen.io/mehra_as/pen/dGdRVq

thanx

3 Answers

Steven Parker
Steven Parker
244,190 Points

It looks like you have two separate issues here. First, you set the click hander for the element you query as "button.play":

var play = document.querySelector("button.play");
play.onclick = begin;

But since your button does not have the play class, this handler is never attached.

Tlhen a few lines later, you have this:

var process = document.querySelector("button");
process.onclick = check;

This will attach a click handler to the button (this time without a specific class) for the check method. So even if the the handlerl had been assigned before, the method gets changed here.

The problem is that you are adding two onclick events on a single button. This is causing only one function to run rather than both, in this case it runs the check() function only.

any solution

I would attach the click event to the first function begin() then, I would run the next function inside the begin() function.

var play = document.querySelector("button");
play.onclick = begin;


function begin(){
 check();
 // more code here
}

should i make a loop like (i=0;i<element.children.length;i++) like in todo app