Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-01-2013, 07:44 PM   PM User | #1
m2244
Regular Coder

 
Join Date: Jun 2012
Posts: 129
Thanks: 1
Thanked 1 Time in 1 Post
m2244 is an unknown quantity at this point
How not to add multiple listeners

Hello,

The code below is called when a user clicks a 'Submit' button. The problem is the line highlited below, each time they click it adds an additional listener so the event fires twicethe second time, three times the third time, etc.

What is the best way to avoid adding unwanted listeneres like this?

Code:
function checkAnswer()
{
	alert(chosenAnswer);
	if(chosenAnswer == correctAnswer)
	{		
		var xmlFeedBack = "";
		$(questionArr[questionIndex]).find('answer').each(function(){
			if($(this).find('aText').text() == chosenAnswer)
			{
				xmlFeedBack = $(this).find('feedback').text();
				alert(xmlFeedBack);
			}
		});

		$("#feedback").html('<img src="../js_know_check/course_assets/kcbtnsprite.png" /><br/>' + xmlFeedBack + "<br/><br/>Click This Message Box to Continue.");
		questionIndex++;
		$("#feedback").click(function (evt) {alert("You clicked the message box! Great");displayQuestion()});
		numCorrect++;
	}
}
m2244 is offline   Reply With Quote
Old 02-01-2013, 07:46 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 950
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
At the start of the code, set a global variable to false. Then, when the link is clicked, set it to true. Put a conditional around the highlighted line - if variable = false { code }. That way, if the variable is true, it won't fire.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 02-02-2013, 11:45 AM   PM User | #3
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,882
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
I would just define the click handler outside the checkAnswer() function.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 02-02-2013, 10:47 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Another alternative would be to have the event listener remove itself again when it runs - that way it can only run once for each time it is added.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:41 AM.


Advertisement
Log in to turn off these ads.