Hello Experts is there any way to remove existing p tags inside the class? if it is more than 1 using JavaScript.
Example:
<div class='classname'>
<p>Lorem ip purus hendrerit quam. Mauris convallis dolor vel ex placerat, non imperdiet dolor lacinia. </p>
<p>Lorem ipsum m ip purus hendrerit quam. Mauris convallis dolor v </p>
<p>Lorem ipsum m ip purus hendrerit quam. Mauris convallis dolor v </p>
<p>Lorem ipsum m ip purus hendrerit quam. Mauris convallis dolor v </p>
</div>`
Expected Result:
<div class='classname'>
<p>Lorem ip purus hendrerit quam. Mauris convallis dolor vel ex placerat, non imperdiet dolor lacinia. </p>
2
Answers
Using
document.querySelectorAll('.classname > p')
it will return allp
elements,then from second element remove itUpdate
If there are more
divs
,then you can using followingIf you need to enforce this every time, you might not even need to check how many
<p>
elements are inside the classname, and just remove all but the first child.This code takes advantage of the CSS selector
:nth-of-type
which will select allp
elements (In case you want to have other type of tags in there)And also the functional notation
n+2
which offsets our selection to spare the first child.