Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation trying to passing variables to a function

    I am trying to practice passing arguments to functions and thought up this trial.

    Ok now I am trying something new, for me, with the drop down list. I am trying to create a function to use with my drop down lists. The function is going to open up what ever URL that is set for the value of the selected item in the list no matter which drop down list calls the function. I want it so that i can use the same function for any drop down list. Below is the code I have so far and thanks in advance for your help.

    Code:
    <form name="DropDown2">
    <p><select name="example3" size="1">
    <option value="http://www.google.com">Google</option>
    <option value="http://www.yahoo.com">Yahoo</option>
    </select>
    
    <a href="JavaScript:myGo(DropDown2, example3)"><img src="goBtn.bmp"></a>
    </form>
    
    <script type="text/javascript">
    <!--
    
    function myGo(formName, selectName)
    {
       window.open(document.formName.selectName.options[document.formName.selectName.options.selectedIndex].value)
    }
    
    //-->
    </script>
    It is my understanding that I do not have to declare function arguments as any specific type. If I take out the variables from the function and put in the specific form name and select name the function works fine.

    So my question is: Is there a way to pass in the form and select to a function? Or is it that I am messing up on the calling of the function?

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,511
    Thanks
    3
    Thanked 504 Times in 491 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    </head>
    
    <body>
    <form name="DropDown2">
    <p><select name="example3" size="1">
    <option value="http://www.google.com">Google</option>
    <option value="http://www.yahoo.com">Yahoo</option>
    </select>
    
    <a href="javascript:myGo('DropDown2', 'example3')"><img src="goBtn.bmp"></a>
    </form>
    
    <script type="text/javascript">
    <!--
    
    function myGo(formName, selectName){
     obj=document[formName][selectName]; // saves typing plus [] convers string to objecy
     window.open(obj.options[obj.options.selectedIndex].value,'','')
    }
    
    //-->
    </script>
    </body>
    
    </html>

  • #3
    JUD
    JUD is offline
    New Coder
    Join Date
    Jul 2005
    Location
    I'm right here
    Posts
    92
    Thanks
    0
    Thanked 2 Times in 2 Posts
    It would probably be a lot simpler to use onChange event handlers in your select fields.

    Something like this:

    PHP Code:
    <form name="DropDown2">
    <
    p><select name="example3" size="1" onChange="myGo(this)">
    <
    option value="http://www.google.com">Google</option>
    <
    option value="http://www.yahoo.com">Yahoo</option>
    </
    select>
    </
    form>

    <
    script type="text/javascript">
    <!--

    function 
    myGo(sel)
    {
       
    window.open(sel.options[sel.selectedIndex].value)
    }

    //-->
    </script> 
    This would call the myGo script as soon as you change the select field so there is no need for your submit button.

    Hope that helps
    JUD

  • #4
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks vwphillips! I am unfamiliar with the way you used the following.

    Code:
    obj=document[formName][selectName]
    What is it doing? I will have to look this up to see what it is. Thanks again.


    And Jud I was planning on moving the code to the onChange event but I wanted to get it working this way so that I knew it was not the way i was implementing the onChange event that was messing it up. Besides, with the way that I am trying to do it now doubles my learning by giving me practice in using images as "buttons". Thank you for the advice though. I do appreciate it.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    it is enough to use select's value, as always the select's value (if it is not multiple) is the same with the selected option's value

    window.open(obj.value,'','')

    Thanks vwphillips! I am unfamiliar with the way you used the following.
    In fact the complete reference is

    document.forms[name_or_index].elements[name_or_index]

    The square brackets notation is very useful in case of evaluating or to dynamically handle the attributes
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,511
    Thanks
    3
    Thanked 504 Times in 491 Posts
    // saves typing plus [] convers string to objecy
    expanding on above

    obj=document[formName][selectName]

    you need to pass the names as strings to the function
    The [] converts thes these strings to objects

    so equates to

    obj=document.DropDown2.example3


  •  

    Posting Permissions

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