Home > Blockchain >  Filter jQuery object by class AND no tags
Filter jQuery object by class AND no tags

Time:10-15

using the following code:

// Get the highlighted content
let $content = $(editor.selection.getContent());

I get the content that is currently highlighted in TinyMCE. When I print out the value of $content in the Console window I get the following:

$content
jQuery.fn.init(5)
    0: span.element.text-element
    1: text
    2: span.element.text-element
    3: text
    4: span.element.text-element
    length: 5

what I'm trying to do is verify if all the objects are either of type .text-element or text. So I am performing the following $content.filter('.text-element').length and compare it to $content.length. If they are equal my object is good. But of course just filtering on '.text-element' only gives me 3 elements.

I have also tried $content.filter('.text-element,:not([class]').length which doesn't work.

How do I return all '.text-element's & elements without a class?

I'll add a TinyMCE tag.

CodePudding user response:

You can achieve your goal by passing a function to filter(). The function can check for the presence of the element, or check the nodeType of the element:

let $filtered = $foo.filter((i, el) => el.classList.contains('text-element') || el.nodeType == Node.TEXT_NODE);
  • Related