onChange issue with select dropdown

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • idj
    New Member
    • Mar 2008
    • 3

    onChange issue with select dropdown

    Hi, I'm using the following code to redirect to a new page with a select dropdown, which works fine:

    [HTML]<script type="text/javascript">
    <!--
    function go(){
    location=
    document.FRM_NA ME.VALUE_NAME.
    options[document.FRM_NA ME.VALUE_NAME.s electedIndex].value
    }
    //-->
    </script>

    <form action="" method="post" name="FRM_NAME" >
    <select name="VALUE_NAM E" id="VALUE_NAME " onChange="go(); ">
    <option value="001.html ">001</option>
    <option value="002.html ">002</option>
    <option value="003.html ">003</option>
    </select>
    </form>
    [/HTML]
    The problem is when I have multiple select dropdowns, I'd like to use the same function with vars for FRM_NAME & VALUE_NAME!

    eg:

    onChange="go(FR M001, VAL001);" and onChange="go(FR M002, VAL002);"

    As a complete newbie to JS, I'd appreciate any help and apologise if this is very basic.

    Thanks in advance.

    KR idj
    Last edited by acoder; Mar 13 '08, 06:53 PM. Reason: Added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Welcome to TSDN and to JavaScript programming!

    No need to apologise for basic questions.

    Just use arguments for the function and use the forms and elements arrays:
    [code=javascript]function go(frm, sel) {
    location.href = document.forms[frm].elements[sel].value;
    }[/code]

    Comment

    • idj
      New Member
      • Mar 2008
      • 3

      #3
      Hi Acoder, thanks for the reply.

      I get an error:

      Error: 'document.forms[...].elements' is null or not an object

      I've used your supplied JS and the following HTML:

      Code:
      <form action="" method="post" name="FRM_NAME">
      <select name="SELECT_NAME" id="SELECT_NAME" onChange="go(FRM_NAME, SELECT_NAME);">
      <option value="001">001</option>
      <option value="002">002</option>
      <option value="003">003</option>
      </select>
      </form>
      Any thoughts?

      KR ijd

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        hi ...

        you have to pass the params as string values (actually in your code you pass variable-names that refer to undefined values in this case), so use the following:

        [HTML]onChange="go('F RM_NAME', 'SELECT_NAME'); "[/HTML]
        kind regards

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Yes, and put the ".html" back in the option values or append it in the function.

          Comment

          • idj
            New Member
            • Mar 2008
            • 3

            #6
            Thanks Guys for all your help. It works now :-). KR idj

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              You're welcome. Glad it's working. Post again anytime should you have any more questions.

              Comment

              Working...