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

Array to string, I get [object HTMLParagraphElement][object HTMLUListElement] instead of value / content

I have an array from a parse

odg = tinymce.activeEditor.getContent({ format: "HTML" });

    const parser = new DOMParser();
    const parsedDocument = parser.parseFromString(odg, "text/html");
    const parsedContent = parsedDocument.querySelectorAll(
        "body:not(first-child) > *"
    );


 let array2 = Array.from(parsedContent);

I get:

Array [ p, ul]
0: <p>​
1: <ul>
length: 2
<prototype>: Array []

When I change into string

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

let contentString = array2.join("");

I get:

[object HTMLParagraphElement][object HTMLUListElement]

Instead of: <p>something</p><ul><li>something else</li></ul>

What am I missing?

>Solution :

You can access outerHTML

let odg = document.querySelector('container').innerHTML
const parser = new DOMParser();
const parsedDocument = parser.parseFromString(odg, "text/html");
const parsedContent = parsedDocument.querySelectorAll(
  ".findme > *"
);


let array2 = Array.from(parsedContent);
console.log(array2.map(m => m.outerHTML).join(''));
<container>
  Ignore me
  <div class='findme'>
    <div>This </div>
    <p>That</p>
  </div>
</container>
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