By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,654 Members | 1,554 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,654 IT Pros & Developers. It's quick & easy.

onClick woes

P: 10
Hi Everyone,

I have a problem with my javascript code. I am trying to pass a parameter to the checkAnswer function during using the onClick event handler.

I wrote the following lines of code:

Expand|Select|Wrap|Line Numbers
  1. var option2= document.createElement("input");
  2.     option2.type = "button";
  3.       option2.name = alldetails[3];
  4.       option2.id = "test123";
  5.       option2.value = alloptions[1];
  6.       tr_a.appendChild(td(option2));
  7.       var testx = option2.name;
  8.   //    alert(testx);
  9.     option2.onclick = new Function('checkAnswer(' + testx + ')'
  10. )
  11.  
  12.  
Where alldetails[3] = “Europe” (I string value in an array).

Expand|Select|Wrap|Line Numbers
  1. function checkAnswer(response)
  2.     {    
  3.         alert(response);
  4.         }
  5.  
When I click my button, nothing happens. The firefox error console says

“Error: Europe is not defined”



Any help would be apprectiated. I basically want the value from the array to be passed to another function upon clicking the button.
Nov 3 '07 #1
Share this Question
Share on Google+
1 Reply


gits
Expert Mod 5K+
P: 5,390
hi ...

welcome to TSDN ...

try:

Expand|Select|Wrap|Line Numbers
  1. option2.onclick = new Function('checkAnswer("' + testx + '")';
kind regards

ps: i assume you always have a string-value to pass.
Nov 3 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.