BUG: listener registration not works due Select2
See original GitHub issueI found Select2 preventing registered event listener to be called. There is duplicate issue to this but without any details or code samples.
Issue – onchange or addEventListener listener on form that employs Select2 is never called.
Example:
In example you can observe this:
- There are six forms.
- Three of them employs Select2 (first three).
- Only one (first) form of first three ones with inline registration calls the listener.
- Rest of the forms are the same as the first three except they don’t employ Select2. They serves verification purpose.
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<script>
$(document).ready(function() {
$('.stoppingPropagationElem').select2();
});
function onChange_Function(obj) {
let text;
if (obj.currentTarget != undefined) {
text = obj.currentTarget.name;
} else {
text = obj;
}
document.body.appendChild(getPwithText(text));
}
function getPwithText(text) {
let node = document.createElement("P");
let textnode = document.createTextNode(text);
node.appendChild(textnode);
return node;
}
window.addEventListener("load", function() {
document.forms["someForm2"].onchange = onChange_Function;
document.forms["someForm3"].addEventListener("change", onChange_Function);
document.forms["someForm5"].onchange = onChange_Function;
document.forms["someForm6"].addEventListener("change", onChange_Function);
});
</script>
</head>
<body>
<form name="someForm" onchange="onChange_Function(this.name)">
<select class="stoppingPropagationElem">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</form>
<form name="someForm2">
<select class="stoppingPropagationElem">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</form>
<form name="someForm3">
<select class="stoppingPropagationElem">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</form>
<form name="someForm4" onchange="onChange_Function(this.name)">
<select>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</form>
<form name="someForm5">
<select>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</form>
<form name="someForm6">
<select>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</form>
</body>
</html>
StackOverflow: https://stackoverflow.com/questions/53304417/js-onchange-works-addeventlistener-does-not
JSFiddle: https://jsfiddle.net/azgsvuj5/.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Common problems - The jQuery replacement for select boxes
Select2 does not function properly when I use it inside a Bootstrap modal. ... This issue occurs because Bootstrap modals tend to steal...
Read more >Select2 doesn't work when embedded in a bootstrap modal
Bootstrap registers a listener to the focusin event which checks whether the focused element is either the overlay itself or a descendent of...
Read more >select2 | Yarn - Package Manager
Select2 is a jQuery based replacement for select boxes. It supports searching, remote data sets, and infinite scrolling of results.
Read more >Options - Select2 - JeeSite
Select2 will register itself as a jQuery function if you use any of the distribution builds, so you can call .select2() on any...
Read more >select2 - NPM Package Versions - Socket - Socket.dev
4.0.2 · New features / Improvements · Bug fixes · Documentation · Translations.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Why close this if it is a confirmed bug? Maybe stalebot needs to turn itself off if the “confirmed bug” label is applied so at least its clear to folks this is a continuing issue.
FWIW, I used the example in the makandra link provided by @orthener and updated it to use native dispatch and a few other tweaks.