How to extract text in HTML node using wp.richtText
-
Assume I have the following HTML string:
Please leave a message in <a href="#comments">the comments section below</a>. Thanks!Now let’s assume I want to emphasize some text in the previous message. In particular, I’d like to make the following sentences in bold:
- leave a message in the comments section
- Thanks!
If I do so using the Block editor, the resulting HTML is as follows:
Please <strong>leave a message in </strong><a href="#comments"><strong>the comments section</strong> below</a>. <strong>Thanks!</strong>As you can see, there are three
strongtags:- a portion of my first sentence (which is outside the
atag) - another portion of my first sentence (which is inside the
atag) - my second sentence
Now let’s assume I want to get get all the highlighted sentences. What I want to retrieve are the original sentences:
- leave a message in the comments section
- Thanks!
but accessing the
textContentofstrongtags would return me three sentences instead:- leave a message in
- the comments section
- Thanks!
How can I use
wp.richTextto retrieve my highlighted sentences? I’m assuming this is something we can do, as the Block Editor can toggle the format of a whole sentence even if it is split in multiple nodes (as shown in the example).
The topic ‘How to extract text in HTML node using wp.richtText’ is closed to new replies.