Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript_howto thread: FW: Passing parameters


Message #1 by "arun" <arun@l...> on Mon, 22 Apr 2002 16:39:28 -0700
Hello arun

The reason you got the error choise1 is undefined is because of the line
document.write ('<input type=radio name="rbtn" value="ch1" onClick="return
check(choise1,answer)">');

What was happening was that JavaScript was writing a string, and it sees 
choise1 and answer as a string. It does not know that what you really 
wanted.

You have to let JavaScript know that you want to 'interpolate' it - which 
means act uppon it, and not just treat it as it is literally.

So you have to break up the line like this: -
document.write('<input type="radio" name="rbtn" value="ch1" 
onClick="return check(' +choise1 +answer +')">');

However there is still is problem here because when you click the radio 
button you are still passing something undefined because you need to pass 
a string to the check function - its a bit complicated until you a have 
messed around with concatenting (joining together)
for hour and hours.

Anyway you end up with a frightening looking line below b4 it will work

document.write('<input type="radio" name="rbtn" value="ch1" 
onClick="return check(' +"'" +choise1 +"'" +', ' +"'" +answer +"'" +')">');

I am not sure what you are trying to do ultimately here with this but I 
would of thought there is an easier way where you would not end up with 
such a line like above. But just keep playing around to begin with thats 
all you can do.

here is the script to replace yours - if you need any further help let me 
know!!

hope this helps and I have not been too confusing (i confuse often 
myself ;)

ps - you do end up with the wrong answer!!



<script language="JavaScript">

function display(question, choise1, choise2, choise3, choise4, choise5, 
answer)
{


document.write(question);
	document.write("<p>");
	document.write('<input type="radio" name="rbtn" value="ch1" 
onClick="return check(' +"'" +choise1 +"'" +', ' +"'" +answer +"'" +')">');


}

function check(one, answer)
{
	alert("one has the value of " +one);
	alert("answer has the value of " +answer);
	if (one == answer) {
		alert('Correct !!');
		return true;
	} else {

	alert("Your answer is incorrect")
	}
}


display("The velocity of a wave is 340 m/s. If it has a wavelength of 2.5 
m calculate its frequency.", "8160 Hz", "850 m", "136 Hz" ,"34 
Hz", "","136Hz");

</script>

  Return to Index