Welcome to WebmasterWorld Guest from 54.163.52.98

Forum Moderators: open

Message Too Old, No Replies

Using a selected value to run a JavaScript function...

     

BlobFisk

10:41 am on Aug 22, 2002 (gmt 0)

WebmasterWorld Senior Member blobfisk is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Hi All - any help with this would be most appreciated!

I have a javascript function which basically toggles the visibility of layers.... no bother there!

Ok, now I have a dropdown list, and what I would like to do is use the onChange method in the <select> tag to somehow run this function... doing onChange="showLayer(layername)" is no bother, however, where I am having a problem is that I want to populate the layerName with the selected value of the dropdown box, eg:


<select name="selectme" onChange="showLayer(this.selected)

{or onChange="showLayer(document.form.selectme.value)"}"
<option value="layer1name">1</option>
<option value="layer2name">2</option>
<option value="layer3name">3</option>
</select>

so that showLayer() gets populated with the correct layername and displays that layer... obviously the above does not work! The option values (and the list itself) is being populated by a database query, so the number of options will not be static.

Thanks in advance for any suggestions or tips,

Alex

Iguana

11:06 am on Aug 22, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Selected isn't a property of a <SELECT> - only of an <OPTION>

<select name="selectme" onChange="showLayer(this)">
<option value="layer1name">1</option>
<option value="layer2name">2</option>
<option value="layer3name">3</option>

and then in showLayer

showLayer(x)
{
makevisible(x.options(x.selectedIndex).text)
}

BlobFisk

11:26 am on Aug 22, 2002 (gmt 0)

WebmasterWorld Senior Member blobfisk is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Thanks for that Iguana... didn't seem to work for me though (having said that I didn't mess around with it much).

Typically, having just posted the topic I solved the puzzle!

OK, the swapLayer() finction is contained in an external JS file (where the following code is about to be moved to also!).

<script language="JavaScript">
function showListLayer()
{
showLayer(document.primarylist.mlist.options[document.primarylist.mlist.selectedIndex].value);
}
</script>

Then:

<form name="primarylist">
<select name="mlist" onChange="showListLayer()">

<added>

In fact, you can do away with the extra JavaScript function totally and just use:

<select name="mlist" onChange="showLayer(this.options[this.selectedIndex].value);">

Much neater by far!

</added>

 

Featured Threads

Hot Threads This Week

Hot Threads This Month