/    Sign up×
Community /Pin to ProfileBookmark

onclick event – help me!!!

Hello,

I’m doing my homework now and onclick event drives me crazy.
My intention is when a user clicks an answer(red, green or blue icon), onclick event is triggered and an alert message shows up saying it’s right or not.
However when the window loads, the onclick event is triggered automatically even though it’s not clicked. I don’t know what’s wrong.

My html code is…

<div id=”questions”>
<h2> What do you think my favorite color is?</h2>
<br />
<span id=”red”><img src=”../images/red.png” /> </span>
<span id=”green”><img src=”../images/green.png” /></span>
<span id=”blue”><img src=”../images/blue.png” /></span>
</div>

My javascript code is…

// JavaScript Document
window.onload = load;
var username;

function load() {
username = prompt(“Please enter your name : “, “”);
if (username == “”) {
username = “Guest”;
}
writeUsername();
firstQuestion();
}

// if a user enters name, it shows on the page.
function writeUsername() {
document.getElementById(“username”).innerHTML = “Hello, ” + username + “! Welcome to my site!!!”;
}

function firstQuestion() {
document.getElementById(“red”).onclick = incorrect();
document.getElementById(“green”).onclick = incorrect();
document.getElementById(“blue”).onclick = correct();
}

function secondQuestion() {
}

function correct() {
alert(“answer is right”);
}

function incorrect() {
alert(“answer is wrong”);
}

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@pactor21Sep 11.2011 — From the look of it, you're basically chain-triggering Every event when the page loads. Try to put the onclick event on the span elements instead of defining them inside the firstQuestion function.

First glance at the code tells me load() gets called when the page loads which I'm sure is intended. What you didn't have in mind happening is load() function calls for firstQuestion() which has an onclick event. So everything happens one after another in domino action.
Copy linkTweet thisAlerts:
@captsigSep 11.2011 — You must remove the () from incorrect() in the following:

document.getElementById("red").onclick = incorrect();

document.getElementById("green").onclick = incorrect();

document.getElementById("blue").onclick = correct();

captsig
Copy linkTweet thisAlerts:
@GrapeloverauthorSep 11.2011 — Thank you for the replies.

I removed () from correct() & incorrect() function calls.

Now it works.

Thank you very, very much for your helps!!!
×

Success!

Help @Grapelover spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...