/    Sign up×
Community /Pin to ProfileBookmark

onClick problem

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.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@FangNov 03.2007 — Function('checkAnswer("'+ testx +'")');
Why not pass [I]this.name[/I]?
×

Success!

Help @DennyLoi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...