Miguel Machado
Miguel Machado

Reputation: 298

JQuery select2 e.val undefined on change

My select2 on change event does not fire e.val.

code:

$(".clienteselect").select2({
        width: "200",
        placeholder: "<?php echo $dicescolhecli; ?>",
})
.on("change",function(e){
    console.log(e.val) ;
});

HTML

<select class="clienteselect">
    <option></option>
    <option value="1"> Manuel Silva </option>
    <option value="2"> Luis Antonio </option>
    <option value="3"> Jose Rodrigues </option> 
</select>

e.val always returns undefined. Did I miss something?

Upvotes: 2

Views: 2000

Answers (1)

Chtioui Malek
Chtioui Malek

Reputation: 11515

e is a reference to the change event you subscribed to, you can use the target (html element) to get your select2 component and find the value :

$(".clienteselect").select2({
  width: "200",
  placeholder: "Select",
}).on("change",function(e){
  console.log($(e.target).select2("val"));
});

hope this helps.

Upvotes: 4

Related Questions