0

Following code is working with Firefox browser but onClick is not working with Chrome and Safari.

Is there anything missing in the following code. If anyone can help so that It can work with cross browser.

Thanks.

<html><body><form method="post">
<select>
<option value="aa">Please select</option>
<option value="pst" onClick="populate();">Select Existing State</option>
</select>

<select name="abc" id="example-select" style="width: 160px;"></select>

</form>

<script type="text/javascript" language="javascript">
    var example_array = {state1 : '1# First State', state2 : '2# Secondstate', state3 : '3# Third State'}; 

    function populate() {
        var select = document.getElementById("example-select");
        for(index in example_array) {
            select.options[select.options.length] = new Option(example_array[index], index);
        }
    }
</script>
</body></html>
Pujan
  • 3,154
  • 3
  • 38
  • 52
  • onClick is not working with Chrome and safari but fine with firefox – Pujan Dec 07 '11 at 07:37
  • possible duplicate of http://stackoverflow.com/questions/4340690/javascript-onclick-alert-not-working-in-chrome – Serge Dec 07 '11 at 07:48

1 Answers1

3

well the onclick seems have some problems on webkit try to bind populate with an onchange

pna
  • 5,651
  • 3
  • 22
  • 37
  • @PujanSrivastava you need to put the onchange on `select` and not on `option` like so http://jsfiddle.net/Ka3YK/3/ – Serge Dec 07 '11 at 07:52
  • But how to call different JS functions on different – Pujan Dec 07 '11 at 07:58
  • if you look at the jsFiddle you'll see that there's a `this` in the populate call, simply do something like populate(e) { alert(e.value) ... } and you'll get the info you need, if you need more help start a new question – Serge Dec 07 '11 at 08:01