views:

49

answers:

3

Hi guys, Am a bit new to javascript. This question might sound a bit too silly, but I'm not able to figure it out why the following doesn't work in IE and works in firefox..

<select multiple="multiple">
 <option value="tx" onClick="alert('tx');">Texas</option>
 <option value="ak" onClick="alert('ak');">Alaska</option>
 <option value="ca" onClick="alert('ca');">California</option>
 <option value="ws" onClick="alert('ws');">Washington</option>
 <option value="tn" onClick="alert('tn');">Tennessee</option>
</select>

The alert doesn't come up in IE (I'm using IE8). But it works in firefox!!!!!

+2  A: 

This is because IE doesn't register a click event when you select a new option in a select field (guessing). Instead, you should use the onBlur event (and put the code into your javascript instead) like so (assuming jQuery):

<script type='text/javascript'>
  $(document).ready(function(){
    $('select#state').bind('blur', function(){
      alert(this.val());
    });
  });
</script>

<select id='state' multiple="multiple">
  <option value="tx">Texas</option>
  <option value="ak">Alaska</option>
  <option value="ca">California</option>
  <option value="ws">Washington</option>
  <option value="tn">Tennessee</option>
</select>
Daniel Mendel
I'm sorry I entered the question wrong. I dont have any jquery at all. COnsidering simple javascript and html select.. Is there a way to make it work in IE?
Rocky
Understood, the answer by Robusto is the most direct, valid, straight javascript solution for the code you posted.
Daniel Mendel
+4  A: 

I would use the onchange event:

<select multiple="multiple" onchange="alert(this.options[this.selectedIndex].value)">
 <option value="tx">Texas</option>
 <option value="ak">Alaska</option>
 <option value="ca">California</option>
 <option value="ws">Washington</option>
 <option value="tn">Tennessee</option>
</select>

Although Daniel Mendel's solution is perfectly valid.

Robusto
Its great when I have a single function to be called. But what if I need to assign different functions for different options?
Rocky
That's what the selectedIndex will tell you. You can get the actual option element from that and do something different with it *within the same function*. That is a feature, not a drawback. For simplicity (and following your lead) I put the entire handler in the event listener attribute, but really you would simply pass "this" to an external function. Or use jQuery and assign it a function literal.
Robusto
change does not fire until the select loses focus, some browsers do fire it as you select with up/down arrows
Juan Mendes
@Juan Mendes: The example I gave fires on all flavors of change, including click and up/down arrows, etc.
Robusto
+1  A: 

According to w3schools, the option tag does support an onclick attribute. I tried with with bottom of the barrel IE6 and this doesn't seem to be the case.

The simplest way to do this would be:

<select multiple="multiple" onchange="alert(this.value);">
 <option value="tx">Texas</option>
 <option value="ak">Alaska</option>
 <option value="ca">California</option>
 <option value="ws">Washington</option>
 <option value="tn">Tennessee</option>
</select>

This is not exactly what you are after, but should be pretty close.

EDITS

It would just take more work:

<select multiple="multiple" onchange="
    switch (this.value){
      case 'tx': funcOne(); break;
      case 'ak': funcTwo(); break;
      etc...
   }
 ">
 <option value="tx">Texas</option>
 <option value="ak">Alaska</option>
 <option value="ca">California</option>
 <option value="ws">Washington</option>
 <option value="tn">Tennessee</option>
</select>

At this point it would be appropriate to wrap the onchange into a function in a js file instead of embedding it in the html.

Mark
Thanks Mark, that looks better. But what if I need to assign different functions for different options? I cant make this work right?
Rocky
@Rocky, see edits above.
Mark
Thanks a lot Mark. That should do it I guess
Rocky