6

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!!!!!

3
  • That has nothing to do with jQuery. Commented Oct 21, 2010 at 15:09
  • possible duplicate of Assign click() to a child control in jquery Commented Oct 21, 2010 at 15:10
  • I'm sorry I put up the wrong heading in question. Its a simple html select element and javascript to invoke click event which does not work in ie Commented Oct 21, 2010 at 15:15

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

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.

4 Comments

Its great when I have a single function to be called. But what if I need to assign different functions for different options?
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.
change does not fire until the select loses focus, some browsers do fire it as you select with up/down arrows
@Juan Mendes: The example I gave fires on all flavors of change, including click and up/down arrows, etc.
2

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>

2 Comments

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?
Understood, the answer by Robusto is the most direct, valid, straight javascript solution for the code you posted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.