DEV Community

Cover image for jQuery Clear innerHTML
CyberBotMachines
CyberBotMachines

Posted on • Updated on

jQuery Clear innerHTML

TL;DR Summary

Most important thing you need to remember when trying to clear innerHTML with jQuery is that you do not touch innerHTML at all.

What you need to do is use .html() function that is already available in jQuery.

Here is the code example:

<div id="container">
  <div class="myclass">HTML one</div>
  <div class="myclass">HTML two</div>
  <div class="myclass">HTML three</div>
</div>

<script>
  // this is how you can read HTML
  $('#container').html();

  // this is how you delete HTML
  $('#container').html('');
</script>
Enter fullscreen mode Exit fullscreen mode

And that is all you need to know about clearing innerHTML with jQuery.

And if you want to speed up your coding career then check out my website for more info.

Until next time,
Will
Senior Software Developer

Discussion (0)