Wrox Programmer Forums
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 3rd, 2007, 11:22 AM
Registered User
 
Join Date: Nov 2007
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.



 
Old November 3rd, 2007, 01:13 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Installation woes gcallaway Beginning PHP 9 March 31st, 2008 07:05 PM
Array woes hillbilly geek PHP How-To 2 January 8th, 2006 12:28 AM
Form woes komputatek Access 2 March 15th, 2005 11:47 AM
Installation woes mattm BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 2 June 26th, 2003 03:40 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.