View Single Post
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