Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running Javascript function from combo box

Status
Not open for further replies.

daveh

Technical User
Jun 22, 2000
57
GB
Hi,

I know how to have a combo box that, after selection (onchange), changes the window.location to be the value (a basic nevigation combo).

However, I have a series of different javascript functions within the page that I want to be called based on the value selected in the combo boxes.

ComboItem1 - javaScript1()
ComboItem2 - javaScript2()
ComboItem3 - javaScript3()

I don't have access to the header of the HTML, so I just want to have all the code within the onchange event handler.

Any ideas?
Thanks.
David.
 
Instead of coding 3 functions, why not just code 1 big function and pass the selected index of your pulldown to that function. Then just have some conditional statement in the function to do the appropriate action. Something like this:
Code:
<script language="javascript">
function blah(num) {
   if (num == 0) {
      //code if the first is selected
   }
   if (num == 1) {
      //code if the 2nd is selected
   }
   //etc.....
}
</script>
<select onchange="blah(this.selectedIndex)">
<option>blah 1
<option>blah 2
<option>blah 3
.
.
.
<option>blah n
</select>

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
You could encode the option in the url of the page you are calling. To call page.html with argument 1, use /page.html?argument=1 and so forth. In the target page, have a hidden field
Code:
<input name="argument" type="hidden">
which you can then access as
Code:
document.getElementById('argument').value

Yours,

fish

&quot;As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilkes
 
Awesome guys thanks for your help. I ended up using the 1 big function method, but I have saved both for future reference, as I can see both are potentially very useful!

Thanks!
Dave.
 
You're welcome

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top