html

Strip HTML using DOM methods

  • Author: guillimon

DataTables has a built in type called html which will strip HTML tags from a search string, but it doesn't cope with nested HTML inside another element's attributes (for example DOM0 events with have HTML in them). This plug-in function overrules the built-in method and provides complete HTML tag removal.

Note that this function is not included in DataTables by default because it is slightly slower than the built-in method, which is good enough for by far the majority of use cases.

Plug-in code

(function () {

var _div = document.createElement('div');

jQuery.fn.dataTable.ext.type.search.html = function ( data ) {
    _div.innerHTML = data;

    return _div.textContent ?
        _div.textContent.replace(/\n/g," ") :
        _div.innerText.replace(/\n/g," ");
};

})();

CDN

This plug-in is available on the DataTables CDN:

JS

Note that if you are using multiple plug-ins, it is beneficial in terms of performance to combine the plug-ins into a single file and host it on your own server, rather than making multiple requests to the DataTables CDN.

Version control

If you have any ideas for how this plug-in can be improved, or spot anything that is in error, it is available on GitHub and pull requests are very welcome!

Example

$(document).ready(function() {
      $('#example').dataTable({
          "columnDefs": [
              { type: "html", target: 0 }
          ]
      });
  } );