Home Section Blog Website JavaScript Option Select
JavaScript Option Select PDF Print E-mail
Blog - Website
Written by Dennis Reinhardt   
Saturday, 06 June 2009 15:39

I have been stumped now and in the past getting JavaScript to read a selected option from a form.  What is troubling are methods which work in FireFox but will not work in Internet explorer.  Here, I am posting the only way I know which works in both browsers (plus Safari, as a bonus):

 

--- working JavaScript option select ---
<html><head>
<script>
  function call_js(one,two){
     show.innerHTML = one + " " + two;
  }
</script></head><body bgcolor=#dbe8fa >

<form><select
  onChange=call_js("Hello",this.value)>
<option></option>
<option value=world>world</option>
<option value=Gaia>Gaia</option>
<option value=planet>planet</option>
</select></form>
<div id=show></div></body></html>
------------------ end -----------------

 

Note that the "this" parameter has to access the "option" element, not the "select" element.  From my experimentation, it appears that it does not matter in FireFox because it appears to propagate the "option" to the"select" so it does not matter which "this" is used.  Not so for IE.  The "option" does not appear to be propagated and the counter-intuitive code above must be used.

Each option is assigned a value and "this.value" assumes the value selected.

Copy/paste the code above to test.  When working, it will display "Hello world", "Hello Gaia", or "Hello planet" as selected.  

Trackback(0)
Comments (0)Add Comment

Write comment

busy
Last Updated on Saturday, 06 June 2009 16:07
 
Copyright © 2010 TextToolKit. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.