javascript and drop down lists

kasio99

New Member
I'm trying to assign a behaviour to a drop down list.

Been fiddling with it for a long time now and can't get it to work......ARHG!

I have pasted the relevant html javaSript below!

Please can somebody tell me where i have gone wrong?

html.......

<select name="eachSegment" id="eachSegment">
<optgroup label = "Segments">
<option value="segment1">Segment1</option>
<option value="segment2">Segment2</option>
<option value="segment3">Segment3</option>
<option value="segment4">Segment4</option>
<option value="segment5">Segment5</option>
<option value="segment6">Segment6</option>
<option value="segment7">Segment7</option>
<option value="segment8">Segment8</option>
<option value="segment9">Segment9</option>
<option value="segment10">Segment10</option>
</optgroup>
</select>

JS......

var $ = function (id) { return document.getElementById(id);}

var segSegment = function() {

if (eachSegment == "segment1") {
alert ("1");
} else if (eachSegment == "segment2"){
alert("2");

} else if (eachSegment == "segment3"){
alert("3");

} else if (eachSegment == "segment4"){
alert("4");

} else if (eachSegment == "segment5"){
alert("5");

} else if (eachSegment == "segment6"){
alert("6");

} else if (eachSegment == "segment7"){
alert("7");

} else if (eachSegment == "segment8"){
alert("8");

} else if (eachSegment == "segment9"){
alert("9");

} else if (eachSegment == "segment10"){
alert("10");

} else {
alert("please select a segment");
}
}
window.onLoad = function () {
$("eachSegment").onchange = segSegment;
}
 
Last edited:

PixelPusher

Super Moderator
Staff member
I am not an expert at javascript, however, when i have written functions and variables they look like this:

Code:
function myJsFunction(obj) { 
       /* statement */
}
 
$(obj).click(function() { 
      /* statement */
});

var myNewObj =  /* blah blah */

Have you considered using css for the drop down menu instead of entirely javascript?
 

craftygeek

New Member
You might be better off having a search for a premade drop down menu script (sometime called an accordian menu) - there are plenty out there, Jquery ones will have animations in them as well.
 
Top