<html>
<head>
<title>remove element by tag name using javascript</title>
</head>
<body style="background:lightgray">
<p>Click the button to test:</p>
<button onclick="test()">remove p element</button>
</br>
<textarea id="edit" style="height:200px;width:240px">
<body>
<p>hello world<span>!</span></p>
</body>
</textarea>
<script>
function test() {
var el = document.createElement( 'html' );
var edit= document.getElementById("edit");
el.innerHTML =edit.value;
removeElementByTagName(el,"p");
edit.value=el.innerHTML;
}
function removeElementByTagName(el,tagName) {
var elements=el.getElementsByTagName( tagName );
while (elements[0]) elements[0].parentNode.removeChild(elements[0]);
}
</script>
</body>
</html>
Comments
Post a Comment