42

I have a Jqgrid which dynamically generates selects like this:

    <select id="won" style="width: 65px;">
       <option value="">-WON?</option>
       <option value="1" selected>Bet1</option>
       <option value="2" >Bet2</option>
       <option value="3" >Bet3</option>
    </select>

Each one have a different selected option. I would like to detect when one select changes so I can save it in my database.

I'm trying with:

         $('#won').change(function(){
               alert("PROBANDO");
          });

But is not working at all.

j08691
  • 204,283
  • 31
  • 260
  • 272
user2876368
  • 667
  • 2
  • 6
  • 7

2 Answers2

98

The problem is that the select are created dynamically, then you need to use .on()

try this:

         $(document).on('change','#won',function(){
               alert("PROBANDO");
          });
Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171
37

This should work, be sure you did not forget $(document).ready(function() {

$(document).ready(function() {  
    $('#won').change(function(){
        alert( $(this).find("option:selected").attr('value') );       
    });
 });
yizzlez
  • 8,757
  • 4
  • 29
  • 44
Corinne Kubler
  • 2,072
  • 5
  • 22
  • 34
  • Found this most usefull – Jimmy Obonyo Abor Dec 29 '15 at 22:46
  • 1
    this is waiting for the document to be ready, but if #won is generated dynamically this will fail. The correct answer is to listen for events at a stable, non generated html node, and check the event came from the correct child node, in this case the stable node is "document" and "#won" is the child. – Josiah Oct 30 '18 at 17:49