I’m trying to remove <select>….</select> html element with all of it’s it’s values (option) on click event. like
JavaScript
x
11
11
1
<span>Span First</span>
2
<select>
3
<option>opt 01</option>
4
<option>opt 02</option>
5
</select>
6
<span>Span Second</span>
7
<select>
8
<option>opt 11</option>
9
<option>opt 12</option>
10
</select>
11
Want to output like this:
JavaScript
1
2
1
Span First Span Second
2
I’m using code like:
JavaScript
1
12
12
1
<script>
2
function removeTags(str)
3
{
4
if ((str===null) || (str===''))
5
return false;
6
else
7
str = str.toString();
8
return str.replace( /(<([^>]+)>)/ig, '');
9
}
10
document.write(removeTags('<html> Tutorix is <script> the best <body> e-learning platform'));;
11
</script>
12
Above code working fine but unable to remove value inside <option>opt ..</option>
Can anyone help me please.
Advertisement
Answer
Remove the <select>
elements with .remove()
.
JavaScript
1
1
1
document.querySelectorAll("select").forEach(el => el.remove());
JavaScript
1
10
10
1
<span>Span First</span>
2
<select>
3
<option>opt 01</option>
4
<option>opt 02</option>
5
</select>
6
<span>Span Second</span>
7
<select>
8
<option>opt 11</option>
9
<option>opt 12</option>
10
</select>