How Can I Use Empty Method In JQuery With Examples

admin_img Posted By Bajarangi soft , Posted On 13-10-2020

In jQuery empty() method removes the child elements of the selected element(s).So today we discuss how to use empty method in jquery

How Can I Use Empty Method In JQuery With Examples

Step 1:Create index.html file and implement below code.

<div id="div1" style="height:100px;width:300px;border:1px solid black;background-color:yellow;">

    This is some text in the div.
    <p>This is a paragraph in the div.</p>
    <p>This is another paragraph in the div.</p>

</div>
<br>

<button class="btn btn-info">Empty the div element</button>


Step 2:Implement jquery to use empty method.

<script>
    $(document).ready(function(){
        $("button").click(function(){
            $("#div1").empty();
        });
    });
</script>
 

Complete Code For Empty Method In JQuery

<!DOCTYPE html>
<html>
<head>
    <title>How Can I Use Empty Method In JQuery With Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<style>
    body {
        background: darkolivegreen;
    }

</style>
<body>
<div class="container">
    <br><br><br>
    <div class="text-center">
        <h2 id="color" style="color: White">Empty Method In JQuery</h2>
    </div>
    <br>
    <br>

    <div class="well">
        <div id="div1" style="height:100px;width:300px;border:1px solid black;background-color:yellow;">

            This is some text in the div.
            <p>This is a paragraph in the div.</p>
            <p>This is another paragraph in the div.</p>

        </div>
        <br>

        <button class="btn btn-info">Empty the div element</button>
    </div>

</div>
</body>
</html>
<script>
    $(document).ready(function(){
        $("button").click(function(){
            $("#div1").empty();
        });
    });
</script>

Related Post