SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Variable in form reference

    I don't know the terminology. Anyway, I have a quiz like this:

    Do you know the answer?
    Question 1 Yes/No
    Question 2 Yes/No
    etc.

    If the user selects the radio button 'no,' I want to (for now) alert the answer to the question. So, I wrote this function which is called when either radio button is clicked:

    Code:
    function questionClicked(inputname,questionnum) {
    
    if(document.questionform.inputname[1].checked) {
    alert(answers[questionnum]);
    }
    
    }
    But obviously, the script looks for the second radio button named 'inputname' when I want it to look for 'q1' or 'q2' or whatever the variable is. How do I do that?

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You'll find an answer here; but, consider this: the reason you generally pass element names or ids to a function is so that you can 'plug them in' at the other end and retrieve the object you'd like to script. Browser programming is all about objects so, keep in mind, you can pass an object just as easily: from an event handler of the object, it's as close as this. Not the answer in all cases, but it usually makes sense to pass a reference to the necessary object(s), then extract whatever data you need at the other end.
    ::: certified wild guess :::

  3. #3
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good point. I'll change it to this and keep the other in the back of my mind for the future... Thanks, man.


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
  •