0

Here is my plain HTML code.

<style>
 /* style tag goes here*/
</style>
<form method="POST" action="#" id="_form_93_" class="_form _form_93 _inline-form  _dark" novalidate>
  <input type="hidden" name="u" value="93" />
  <input type="hidden" name="f" value="93" />
  <input type="hidden" name="s" />
  <input type="hidden" name="c" value="0" />
  <input type="hidden" name="m" value="0" />
</form>
<script type="text/javascript">
// script tag goes here
</script>

I want to remove <style></style> and <script></script> tag from HTML code so expected result will be.

<form method="POST" action="#" id="_form_93_" class="_form _form_93 _inline-form  _dark" novalidate>
  <input type="hidden" name="u" value="93" />
  <input type="hidden" name="f" value="93" />
  <input type="hidden" name="s" />
  <input type="hidden" name="c" value="0" />
  <input type="hidden" name="m" value="0" />
</form>
6
  • Why ? Do not write css and script then! Commented Jun 16, 2016 at 6:59
  • 1
    api.jquery.com/remove Commented Jun 16, 2016 at 7:00
  • @Rayon Actually this is autoresponder html code and i just want <form></form> code. Commented Jun 16, 2016 at 7:02
  • Is this in a running HTML page? If so, just use elem.parentNode.removeChild(elem); Or do you only have this for a source? Commented Jun 16, 2016 at 7:02
  • @RoryMcCrossan's answer is perfect for what you want to achieve. Commented Jun 16, 2016 at 7:03

2 Answers 2

3

You can remove the tags using this

$('style, script').remove();

Styles will no longer show on screen and both HTML elements will dissapear from the DOM.

BUT, javascript code will still be executed if events have been attached before running the remove() code.

If you're only looking to clean some code returning by an ajax call (for example), it will work fine. If you want to clear the styles and javascript from the page you're already in, some Javascript code might already be attached to elements before you remove the script tag.

Check this fiddle for an example.

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

Comments

0

Using jQuery:

jQuery('style').remove();

jQuery('script').remove();

Using Plain JavaScript:

window.onload = function(){
    var getStyle = document.getElementsByTagName("style");
    getStyle[0].remove();
    var getScript = document.getElementsByTagName("script"); 
    getScript[0].remove();
}

Please find a working fiddle, which shows how to delete h1 tag, Link

Comments

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.