Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get rid of HTML tags after submitting TinyMCE text in react

This is my react code.

import React, { useState, useRef } from "react";
import { Editor } from '@tinymce/tinymce-react';
const PostBlog = () => {
const editorRef = useRef(null);
const [description, setDescription] = useState("");
    const log = () => {
        if (editorRef.current) {
            setDescription(editorRef.current.getContent());
            console.log(description);
        }
    };
return(
<div className="tinymce">
                    <Editor
                        apiKey="kwoypty9nsvg3at4nannkt6kla0ab2e0tp9wtux0f9u6oqpu"
                        onInit={(evt, editor) => editorRef.current = editor}
                        initialValue="<p>This is the initial content of the editor.</p>"
                        init={{
                            forced_root_block:false,
                            selector:"textarea",
                            height: 200,
                            width: 740,
                            menubar: false,
                            toolbar: "bottom",
                            plugins: [
                                'advlist autolink lists link image charmap print preview anchor',
                                'searchreplace visualblocks code fullscreen',
                                'insertdatetime media table paste code help wordcount'
                            ],
                            toolbar: 'undo redo | formatselect | ' +
                                'bold italic backcolor | alignleft aligncenter ' +
                                'alignright alignjustify | bullist numlist outdent indent | ' +
                                'removeformat | help',
                            content_style: 'body { font-family:Helvetica,Arial,sans-serif; font-size:14px }'
                        }}
                    />
                    <button type="button" onClick={log}>Log editor content</button>
                </div>
)

When I click on ‘Log editor content’ I get the output with HTML tags. As the initial value of TinyMCE editor is ‘

This is the initial content of the editor.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

‘, The output I get is the same with HTML tags, but I want the output as text without HTML tags

>Solution :

You can use this cleanup function to remove the HTML tags from a string

const cleanup = (input) => {
  const div = document.createElement('div');
  div.innerHTML = input;
  return div.innerText;
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading