Solved

Simple Script-loading drop down menu

Posted on 2000-03-01
6
168 Views
Last Modified: 2013-12-25
I'd like to create a drop-down menu, but for each selection, I'd like to call a specific javascript.. Sort of like

<html>
<head>
<!-- javascripts here -->

</head>
<body>
<form name="form" >
  <select name="select">
    <option>Select one</option>
    <option>run js1</option>
    <option>run js2</option>
  </select>
</form>
...

Note: it should not require a submit button, since only one item can be selected.

Thanks!
-Sean
0
Comment
Question by:slydon
  • 3
  • 2
6 Comments
 
LVL 18

Expert Comment

by:mgfranz
ID: 2574473
Sure, something like this;

<head>
<script language=javascript>

function JS1()

.... your code

end function

function JS2()

.... more of your code

end function

</head>
<body>

<form name="form" > 
  <select name="select">
    <option>Select one</option>
    <option value="javascript:JS1();">run js1</option>
    <option value="javascript:JS2();">run js2</option>
  </select>
</form>
</body>
</html>

Mark

0
 
LVL 19

Accepted Solution

by:
cheekycj earned 100 total points
ID: 2576892
or do this:

<head>
<script language=javascript>

function JS1() {
..
..
..
}

function JS2() {
..
..
..
}
function handle_select() {
if (document.myform.runselect.selectedIndex==0) {
  JS1();
} else if (document.myform.runselect.selectedIndex==1){

}
}
</script>
</head>
<body>

<form name="myform" > 
  <select name="rnselect" onChange="handle_select();">
    <option>Select one</option>
    <option>run js1</option>
    <option>run js2</option>
  </select>
</form>
</body>
</html>

0
 
LVL 1

Author Comment

by:slydon
ID: 2580787
Tried suggestion 1, as there would be more than 2 options, but something is still wrong.. perhaps its my formatting?
Using the Dreamweaver layer show/hide function as an example..


<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript">
<!--
function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}
//-->
</script>
</head>

<body bgcolor="#FFFFFF">
<p><a href="#" onMouseOver="MM_showHideLayers('Layer2','','hide','Layer1','','show')">open
  layer 1</a></p>
<p><a href="#" onMouseOver="MM_showHideLayers('Layer2','','show','Layer1','','hide')">layer
  2</a></p>
<div id="Layer2" style="position:absolute; width:200px; height:115px; z-index:2; left: 266px; top: 76px; visibility: hidden">gbdgfbgdgvdgf</div>
<div id="Layer1" style="position:absolute; width:200px; height:115px; z-index:1; visibility: hidden">gnfhhgbfbg</div>
<form name="form" >
  <select name="select">
    <option>Select one</option>
    <option value="javascript:MM_showHideLayers('Layer2','','hide','Layer1','','show');">run js1</option>
    <option value="javascript:MM_showHideLayers('Layer2','','show','Layer1','','hide');">run js2</option>
  </select>
</form>
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 19

Expert Comment

by:cheekycj
ID: 2581338
my comment was just an addition to the first one... you are not limited to just two... you just need to extend you if-then-else or use a switch statement.

CJ
0
 
LVL 1

Author Comment

by:slydon
ID: 2593208
Thanks CJ.. I'm going to try it out
-Sean
0
 
LVL 1

Author Comment

by:slydon
ID: 2760824
Took a bit of tweaking but I got it to work reasonably well. Thanks :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question