onClick woes

Discussion in 'JavaScript' started by DennyLoi, Nov 3, 2007.

  1. #1
    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.
     
    DennyLoi, Nov 3, 2007 IP
  2. KatieK

    KatieK Active Member

    Messages:
    116
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    58
    #2
    This strikes me as troublesome:

    Where alldetails[3] = “Europe”
    Code (markup):
    That's always going to evaluate as true, because you've use the single =, which assigns values to variables, and then returns true if successful. You probably want to use the double ==. Ex:

    Where alldetails[3] == “Europe”
    Code (markup):
    (Note that I've posted my code as code by surrounding it with [CODE] and [/CODE].)

    Give that a try.
     
    KatieK, Nov 3, 2007 IP
  3. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Post the entire page in [html][/html] tags.
     
    MMJ, Nov 4, 2007 IP