0

I've got a string that has html tag like this:

var list = "<ul><li><p>example 1st</p></li><li><p>example 2nd</p></li></ul>"

how can I remove every character of The u, p, li tags

so i can get return result in array like this :

['example 1st','example 2nd']

1 Answer 1

2

You can create a div and then find all the text using innerText using querySelectorAll and array#map.

const list = "<ul><li><p>example 1st</p></li><li><p>example 2nd</p></li></ul>";
const div = document.createElement('div');
div.innerHTML = list;
const text = [...div.querySelectorAll('ul li p')].map(element => element.innerText);
console.log(text);

Sign up to request clarification or add additional context in comments.

2 Comments

So this only works for ul, li and p? Not really a good answer IMHO.
I think you meant array.map instead of array#map.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.