I just got the info that my jQuery function is not working on IE nor Edge. In the console I have the message:
Object doesn't support property or method 'closest'
This is the jQuery:
$('body').on('change', 'select', function (event) {
if(event.target.id.indexOf("couche") >= 0) {
$.ajax({
url: "{{ redir2 }}",
type: "POST",
data: {
ident: event.target.id,
value: event.target.value,
iscouche: "True"
},
}).done(function (msg) {
if(msg.nothing == 1) {
var what = event.target.closest('tbody');
$(what).find("tr:gt(0)").remove();
} else {
var add = event.target.closest('tr');
var toremove = msg.toremove.split(" ");
for(var i = 0; i < toremove.length; i++) {
if(toremove[i].length > 0) {
jQuery(toremove[i]).remove();
}
}
jQuery(add).after(msg.ret);
}
});
} else {
$.ajax({
url: "{{ redir2 }}",
type: "POST",
data: {
ident: event.target.id,
value: event.target.value,
iscouche: "False"
},
}).done(function (msg) {});
}
});
Can someone tell me if there's a fix for that?
event.target
is a DOM node, not a jQuery object so has no jQuery methodsIn jQuery instead use
$(this)
which is a jQuery object.I also suggest you don't use the target if you do not need to.
UPDATE: the newer browsers now has the DOM method closest, so OPs code would work in newer browsers except IE.
Here is a fixed jQuery version:
or neater:
closest()
is defined on jQuery prototype, it cannot be used on plain JavaScript object.event.target
is the DOM element on which the event has occurred, to use jQuery methods on it, the element need to be wrapped in the jQuery.Change
to
Yuo must inclose it in
$()
asevent.target
is not a jQuery element