How can I pass the element name into a function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tarek01
    New Member
    • Feb 2010
    • 14

    How can I pass the element name into a function?

    Hi,

    I made a function whereby clicking a radio button will show a hidden radio button sub-group, and when it's unchecked, the radio button sub-group hides again and all the radios in the sub-group are made unchecked.

    The problem is I don't know how to pass the 3rd parameter, 'custom' in this example, into the function.

    HTML:
    Code:
    <input type="radio" name="product" value="standard"
       onclick="showSubGroup(this,'customOptions','custom');" />
    <input type="radio" name="product" value="custom"
       onclick="showSubGroup(this,'customOptions','custom');" />
    <div id="customOptions" style="display:none">
      <input type="radio" name="custom" value="1" />
      <input type="radio" name="custom" value="2" />
      <input type="radio" name="custom" value="3" />
    </div>
    JAVASCRIPT:
    Code:
    function showSubGroup(selection,showHideItem,btnGroup) {
       var parentForm = selection.form;
       if (selection.value == "custom") {
          document.getElementById(showHideItem).style.display = "block";
       } else {
          for (i=0; i < parentForm.btnGroup.length; i++) {
             parentForm.btnGroup[i].checked = false;
          }
          document.getElementById(showHideItem).style.display = "none";
       }
    }
    If I hardcode the name of the input, it works. For example:
    (i=0; i < parentForm.custom.length; i++)
    parentForm.custom[i].checked = false

    How can I pass the element name so it's variable inside the function?

    Thanks in advance.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    use parentForm[btnGroup]

    Comment

    • tarek01
      New Member
      • Feb 2010
      • 14

      #3
      Dormilich, I tried that, but it doesn't work.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        then something’s wrong with your implementation.

        EDIT: correction, parentForm = selection.form is wrong
        Last edited by Dormilich; Jul 16 '10, 09:19 PM.

        Comment

        • tarek01
          New Member
          • Feb 2010
          • 14

          #5
          Dormilich, you're right! That works!! I must've clicked too fast before the javascript finished loading. I checked it in FF and IE, and it works perfectly.

          I left this code as is:
          parentForm = selection.form;


          You da man, Dorm!

          Comment

          Working...