...

View Full Version : trying to passing variables to a function



CodeGnome
12-29-2005, 12:00 AM
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. :)



<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?

vwphillips
12-29-2005, 12:18 AM
<!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>

JUD
12-29-2005, 12:25 AM
It would probably be a lot simpler to use onChange event handlers in your select fields.

Something like this:



<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 :thumbsup:

CodeGnome
12-29-2005, 03:19 AM
Thanks vwphillips! I am unfamiliar with the way you used the following.



obj=document[formName][selectName]


What is it doing? I will have to look this up to see what it is. Thanks again. :D


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". :D Thank you for the advice though. I do appreciate it.

Kor
12-29-2005, 10:07 AM
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

vwphillips
12-29-2005, 10:36 AM
// 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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum