Is there a way to remove certain content from a p
tag with JavaScript?
If I have 5 p
tags with the strings First note
all the way to Fifth note
, I would like to loop through with querySelectorAll
and then use the remove
functionality to remove the string note
from the p
tags.
This is how far I have managed, but I lack the functionality to specify a string in the p
tag to delete:
const pTag = document.querySelectorAll('p')
pTag.forEach(function (p) {
p.remove()
})
You cannot use the remove()
method to only remove part of the string. You can use the replace()
method instead:
const pTag = document.querySelectorAll('p');
pTag.forEach(function(p) {
p.innerHTML = p.innerHTML.replace('note', '');
});
<p>This is the first note</p>
<p>This is the second note</p>
<p>This is the third note</p>
<p>This is the fourth note</p>
<p>This is the fifth note</p>