p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript How-To (http://p2p.wrox.com/forumdisplay.php?f=87)
-   -   onClick woes (http://p2p.wrox.com/showthread.php?t=63498)

DennyLoi November 3rd, 2007 11:22 AM

onClick woes
 
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:

var option2= document.createElement("input");
    option2.type = "button";
      option2.name = alldetails[3];
      option2.id = "test123";
      option2.value = alloptions[1];
      tr_a.appendChild(td(option2));
      var testx = option2.name;
  // alert(testx);
    option2.onclick = new Function('checkAnswer(' + testx + ')'
)


Where alldetails[3] = “Europe” (I string value in an array).

function checkAnswer(response)
    {
        alert(response);
        }

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.




joefawcett November 3rd, 2007 01:13 PM

There are a number of ways:
Code:

option2.onclick = function(){checkAnswer(this.name);};
I think that's neater or
Code:

option2.onclick = new Function("checkAnswer('" + testx + "');");
what you had but your quotes were wrong.

--

Joe (Microsoft MVP - XML)


All times are GMT -4. The time now is 11:33 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.