JS:jQuery Get all nodes, including orphaned text

/ Published in: jQuery
Save to your folder(s)

Reads all the nodes of the object, including orphaned text that's not wrapped into any tag as well as empty text nodes

Copy this code and paste it in your HTML
  1. var $element = $('#my-element');
  2. var $nodes = $element.contents();
  3. $nodes.each(function() {
  4. if(this.nodeType === 3 && $.trim($(this).text())) {
  5. $(this).wrap('<span class="orphan" />');
  6. }
  7. });

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.