Search code examples
javascriptjqueryhtmlrich-text-editorwysihtml5

Get second body text using javascript


Basically HTML elements contain a body tag. In this body tag we add required html content. In my cases HTML file has two body content one for whole html content as usual another one for Rich Text Editor. I need to take Rich Text Editor text using second body.

I have already tried as follows get body tag value from website Get all elements in the body tag using pure javascript

second body html content as follow,

<body marginwidth="0" marginheight="0" spellcheck="true" class="wysihtml5-editor" style="font-variant-ligatures: normal; font-variant-caps: normal; background-color: rgb(255, 255, 255); color: rgb(95, 100, 104); cursor: auto; font-family: &quot;Open Sans&quot;, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; line-height: 18px; letter-spacing: normal; text-align: start; text-decoration: none solid rgb(95, 100, 104); text-indent: 0px; text-rendering: auto; word-break: normal; word-wrap: break-word; word-spacing: 0px;" contenteditable="true">testing</body>

I also tried with class name as follows but shows $(...).text is not a function.

$(".wysihtml5-editor").text()

Please give your suggestions.. Thanks


Solution

  • Try to add jquery library or use javascript

    //by javascript
    var text = document.getElementsByTagName("body")[0].textContent
    alert(text);
    
    //by jquery
    $(document).ready(function(){
        var text = $('body').text();
        alert(text);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <body marginwidth="0" marginheight="0" spellcheck="true" class="wysihtml5-editor" style="font-variant-ligatures: normal; font-variant-caps: normal; background-color: rgb(255, 255, 255); color: rgb(95, 100, 104); cursor: auto; font-family: &quot;Open Sans&quot;, sans-serif; font-size: 14px; font-style: normal; font-weight: normal; line-height: 18px; letter-spacing: normal; text-align: start; text-decoration: none solid rgb(95, 100, 104); text-indent: 0px; text-rendering: auto; word-break: normal; word-wrap: break-word; word-spacing: 0px;" contenteditable="true">testing</body>