Select2 cancel/preventDefault select2:select at specific condition (v.4.0.x)
Asked Answered
Q

1

6

I need to add a Button to every select2 item and prevent the default event so only the button gets triggered.

I have the following code but the normal onSelect event still gets triggered:

select.on('select2:select', test2);

function test2(e) {
    if (e.params.originalEvent.target.classList.contains('TreeButton')) {
        //stop event execution
        e.stopPropagation();
        e.preventDefault();
        return false;
    } else {
        //execute normal
    }
}
Quarterback answered 3/1, 2018 at 14:49 Comment(0)
T
10

Try to catch select2:selecting event:

select.on("select2:selecting", function (e) { 
    if (e.params.args.originalEvent.target.className === 'btn') {
      e.preventDefault();
    }
}

Here is a jsfiddle: http://jsfiddle.net/beaver71/dtjhpnm7/

Thou answered 3/1, 2018 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.