jQuery

[jQuery] jQuery/Method/.empty() - method to clear the contents of the selected element

OOQ 2022. 9. 10. 11:03
728x90
SMALL

#jQuery

#empty

.empty()

.empty() clears the contents of the selected element. Note that the tag remains, just deleting the content.

(Use .remove() to remove the entire element including the tag.)

grammar

.empty()

for example

<h1>Lorem</h1>
$( 'h1' ).empty()

Then it becomes:

<h1></h1>

example

Clicking the button clears the contents of the p element.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>jQuery</title>
    <style>
      p {
        padding: 20px;
        background-color: #eeeeee;
      }
    </style>
    <script src="//code.jquery.com/jquery-3.3.1.js"></script>
    <script>
      $( document ).ready( function() {
        $( 'button' ).click( function() {
          $( 'p' ).empty();
        } );
      } );
    </script>
  </head>
  <body>
    <button>Click To Empty</button>
    <p>Lorem Ipsum Dolor</p>
  </body>
</html>

 

728x90
LIST