SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: onClick problem

  1. #1
    SitePoint Member
    Join Date
    Nov 2007
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DennyLoie View Post
    option2.onclick = new Function('checkAnswer(' + testx + ')'
    Try:
    Code:
    option2.onclick = new Function('checkAnswer("' + testx + '")';
    Tab-indentation is a crime against humanity.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •