Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of javascript `this.closest` in IE11 and Chrome [duplicate]

In IE11 the below code shows the following error, But it is working in Chrome

Object doesn't support property or method 'closest'4:31 PM 09/07/16

assetTable.on("click", "td.clickProgress", function onDataTableRowClicked(event) {
  var tr = this.closest("tr");
  var assetId = tr.id.replace("asset_", "");
  LoadDialog(assetId);
}

However, when I change it to $(this) it is working in both IE and Chrome

assetTable.on("click", "td.clickProgress", function onDataTableRowClicked(event) {
  var tr = $(this).closest("tr");
  var assetId = tr.attr('id').replace("asset_", "");
  LoadDialog(assetId);
}

Any reason behind this compatibility issue between browsers?

like image 1000
Vijai Avatar asked Sep 07 '16 23:09

Vijai


1 Answers

Check out MDN's compatibility table for element.closest. Per the link:

This is an experimental technology Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.

element.closest is an experimental technology not supported on IE. In this case you use JavaScript's this, unmodified. The reason why $(this).closest works is because you are wrapping this into a jQuery object, making it use jQuery's closest function.

like image 90
Andrew Li Avatar answered Oct 23 '22 02:10

Andrew Li