homepage Welcome to WebmasterWorld Guest from 54.163.91.250
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

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




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

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




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

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




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

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>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved