params object is not present when fire event using .trigger("select2:select")

1,487 views
Skip to first unread message

m...@studionoto.com

unread,
May 25, 2015, 4:35:50 AM5/25/15
to sel...@googlegroups.com
Hi there,
I need to access to params object when event is fired.
If I fire the event from dropdown i can access to params object.

If I fire event from script with command:
$("#boxf").val("C").trigger("change")
or
$("#boxf").val("C").trigger("select2:select")

the params object is not present!
How can access to params object in this case ? is it a bug ?
thank you
Max

<body>
<select id="boxf">
</select>

<script>
var ev;
d = [
{ "id": "1", "text": "one" },
{ "id": "2", "text": "two" },
{ "id": "3", "text": "three" },
{ "id": "4", "text": "four" },
];

$f = $("#boxf").select2({ data: d });

$f.on("select2:select", function (e) {

console.log(e);
ev = e;
val = $f.val();
});

</script>
</body>

Kevin Brown

unread,
May 25, 2015, 1:41:27 PM5/25/15
to sel...@googlegroups.com
Hi,

I've left a few comments in-line, it sounds like there might be something wrong.

I need to access to params object when event is fired.

In any event where Select2 triggers it, the params object can be accessed through

    event.params

Where `event` is the first argument passed to the event handler. This only applies to Select2-triggered events though, as Select2 has to manually attach this object to the event when it is triggered.
 
If I fire the event from dropdown i can access to params object.

Why are you trying to trigger an internal Select2 event on your own? You should really not trigger a Select2 DOM event, as they are designed to only be triggered from within Select2.
 
If I fire event from script with command:
$("#boxf").val("C").trigger("change")
or
$("#boxf").val("C").trigger("select2:select")
 
Two things to mention here:

- When you want to tell Select2 to update the state of the selected options, you should trigger the `change` event.
- When you trigger an event using the `trigger` method provided by jQuery, no additional data is passed along with the event.

the params object is not present!

This is because you are not passing along the params object when your are triggering the event. The params object is not magically added on the fly by jQuery, this is included manually when Select2 triggers the event.

Kevin Brown

m...@studionoto.com

unread,
May 25, 2015, 5:17:10 PM5/25/15
to sel...@googlegroups.com, m...@studionoto.com
Hi Kevin,
Thank you very much for the quick response.

I try to explain my problem with an example:
http://jsfiddle.net/0ktqj0by/

In this example, you can disable an option selecting it. You can re-enable only the latest disabled using the button.

I need to access to params.data because I need to disable/enable option after it is created.
I'm using the events handler because this is the only way I found to change the state of "disabled" parameter.
Can you suggest me the right way?
thank you
Max
Reply all
Reply to author
Forward
0 new messages