Do not add onchange attribute to select tag

0

Good day I have two select tags with options created at runtime, the select ones are displayed correctly with their options, I add them with jquery the onchange event so that when a different option is selected, perform a function,

$("#combo1").attr('onchange', "SelectedIndex1(this.value)");
$("#combo2").attr('onchange', "SelectedIndex2(this.value)");

(Where SelectedIndex1 and 2 are the functions that I want the combos to perform when selecting a value) In one of the selections it is done correctly, but I have a select to which the onchange event is not added, some idea of why it does not detect it? Thanks.

    
asked by EriK 22.09.2017 в 17:59
source

2 answers

0

I really do not understand why it does not work like that, I had never tried it, but what you could do is add the event directly, like this:

$("#combo1").on('change', function(){SelectedIndex1(this.value)});
$("#combo2").on('change', function(){SelectedIndex2(this.value)});
    
answered by 22.09.2017 в 18:26
0

But why do you add them as an attribute? Just do it as an event

$("#combo1").change(function(){
    var valor = $(this).val();
});

$("#combo2").change(function(){
    var valor = $(this).val();
});

Greetings.

    
answered by 22.09.2017 в 18:31