Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fire onchange event in chosen prototype javascript select box?

I am using chosen prototype for select box. Now i want to fire onchange event on that select box. here is the link for chosen prototype

How to do this. please help me.

<script type="text/javascript" src="/js/Event.simulate.js"></script>
<script src="/js/prototype.js" type="text/javascript"></script>
<script src="/chosen/chosen.proto.js" type="text/javascript"></script>


<div class="side-by-side clearfix" style="margin-bottom:14px;">

        <select data-placeholder="Select Loacation"  class="chzn-select" tabindex="2" name="source">
          <option value="">Select Loacation</option>
          <option value="">option1</option>
          <option value="">option2</option>
        </select>
  </div>



Event.observe($(".chzn-select"),'change', function(){

//alert($(".chzn-select").chosen());

})
like image 829
Ramesh Kotha Avatar asked Dec 02 '22 00:12

Ramesh Kotha


2 Answers

In this line you have used $() with a class name, which it does not support.

Event.observe($(".chzn-select"),'change', function(){

A class name can be used multiple times so an array is returned from $$(), here is one way to work with arrays.

$$(".chzn-select").invoke('observe', 'change', function() {
    ...
});

I haven't used Chosen before; it's instructions say it needs to be setup so you might have to do this outside of the change event.

document.observe('dom:loaded', function() {
    $$(".chzn-select").each(function(select) {
        new Chosen(select);
    });
});
like image 86
clockworkgeek Avatar answered Dec 04 '22 12:12

clockworkgeek


use the "addEventListener" method on the select box object.

EDIT - here's an example:

document.getElementById('selecboxid').addEventListener('change', SomeFunction(), false);
like image 44
Krik Avatar answered Dec 04 '22 13:12

Krik