Froala - Get HTML

I am trying to get the current HTML from an element using Froala 2.4
My main reason is to compare the original HTML from the new HTML to see if the user has changed something, and if this case triggers a save event.

Here is my current code

console.log($(this).froalaEditor('html.get')) console.log($(this).data('froala.editor')._original_html); 

And here is the conclusion

 <p spellcheck="false">TestString<b>Edited</b></p> TestString<b>Original</b> 

The problem is that I want to get only this from the first output:

  TestString<b>Edited</b> 

(I do not want the paragraph tag to be added in this example)

I could make a short function to take care of this for me, but it seems like I am missing something really obvious regarding how I get the string from Froala.

Help really appreciate!

+6
source share
1 answer

You probably need to save the contents of your editor somewhere:

 var html = $(this).froalaEditor('html.get'); // <p spellcheck="false">TestStringEdited</p> 

And then just separate the HTML tags if I understand you correctly. The easiest way is with the jQuery.text () method :

 var text = $( html ).text(); // TestStringEdited 

Update

Do you need to remove only the top-level <p> tags? Here is a function that implements the filtering of these tags using jQuery. But you can rewrite it and create an array of exception tags, or call it recursively for each child content element. Alternatively, you can use regular expressions as an alternative.

 function stripParagraphs( html ) { var r = ''; $( html ).each(function() { // test each higher-level tag to be <p> if ($( this ).prop( 'tagName' ) === 'P') { r += $( this ).html(); // add contents of <p> to result } else { r += this.outerHTML; // add the whole element to result } }) return r; } 

Living example .

another update

Remove all top level tags.

 function stripTopLevelTags( html ) { var r = ''; $( html ).each( function() { r += $( this ).unwrap().html(); }); return r; } 

Real time example

+7
source

Source: https://habr.com/ru/post/1013267/


All Articles