Sibeesh Passion

Top Menu

  • Home
  • Search
  • About
  • Privacy Policy

Main Menu

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Home
  • Search
  • About
  • Privacy Policy

logo

Sibeesh Passion

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment

  • Build, Deploy, Configure CI &CD Your Static Website in 5 mins

  • Post Messages to Microsoft Teams Using Python

  • Get Azure Blob Storage Blob Metadata Using PowerShell

  • Deploy .net 6 App to Azure from Azure DevOps using Pipelines

JavaScriptJQuery
Home›JavaScript›Remove a DOM element using JQuery

Remove a DOM element using JQuery

By SibeeshVenu
July 1, 2015
1238
0
Share:

Introduction

Hi All, How are you today? In this article we will see how we can remove a DOM element using JQuery. I hope you will like it.

Background

I know we all are working in the client side technologies, especially in JQuery. Sometimes we may need to write more client side codes rather than server side codes. In that case, you may need to remove some DOM elements pragmatically. Here I am going to give you a demo of how we can do this requirement.

Using the code

To start with, as you all know we need to load the JQuery reference.

[js]
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
[/js]

Once you load the reference you are ready to go.

Since this is a demo, we will explain with some steps. Sounds good?. So we will do the following tasks.

  • Add the elements to the DOM
  • Delete the DOM elements using .remove(), .get() functions
  • Shall we start then?

    Add the elements to the DOM

    We need to set our UI first right?

    [html]
    <body id="body">
    Remove a DOM element from the UI using JQuery- Sibeesh Passion
    <br/>
    <br/>
    <p id="addMe">Generate 10 Elements</p>
    </body>
    [/html]

    Add CSS
    [css]
    <style>
    p {
    color: red;
    width: 170px;
    cursor: pointer;
    border: 1px solid #ccc;
    text-align: center;
    }
    #deleteMe {
    color: white;
    width: 170px;
    cursor: pointer;
    border: 1px solid #ccc;
    text-align: center;
    background-color: blue;
    }
    </style>
    [/css]

    So we have set our UI, and now if you run your page you can see output as follows.

    www.sibeeshpassion.com

    Now we will add the needful scripts.

    [js]
    <script>
    $(document).ready(function() {
    $("#addMe").click(function() {
    var html = ‘<table>’;
    for (var i = 1; i <= 10; i++) {
    html += "<tr><p>My Elements</p></tr>";
    }
    html += ‘</table>’;
    $(‘#body’).append(html).append(‘<div id="deleteMe">Delete 5 Elements</div>’);
    $("#addMe").hide();
    });
    $(document).on(‘click’, ‘#deleteMe’, function() {
    for (var i = 1; i <= 5; i++) {
    $(‘#body’).find(‘p’).get(i).remove();
    }
    $("#addMe").hide();
    $("#deleteMe").hide();
    });

    });
    </script>
    [/js]

    As you can see we are adding elements to the DOM with a loop. Once you run the page you can see the output as follows.

    www.sibeeshpassion.com

    And once you click on “Delete 5 Elements” button, 5 DOM elements will be deleted.

    www.sibeeshpassion.com

    The following code block describes how we can use .remove() function.

    [js]
    $(‘#body’).find(‘p’).get(i).remove();
    [/js]

    Complete Code

    [html]
    <html>

    <head>
    <title>emove a DOM element from the UI usign JQuery- Sibeesh Passion</title>
    <style>
    p {
    color: red;
    width: 170px;
    cursor: pointer;
    border: 1px solid #ccc;
    text-align: center;
    }
    #deleteMe {
    color: white;
    width: 170px;
    cursor: pointer;
    border: 1px solid #ccc;
    text-align: center;
    background-color: blue;
    }
    </style>
    <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
    </head>

    <body id="body">
    Remove a DOM element from the UI using JQuery- Sibeesh Passion
    <br/>
    <br/>
    <p id="addMe">Generate 10 Elements</p>
    <script>
    $(document).ready(function() {
    $("#addMe").click(function() {
    var html = ‘<table>’;
    for (var i = 1; i <= 10; i++) {
    html += "<tr><p>My Elements</p></tr>";
    }
    html += ‘</table>’;
    $(‘#body’).append(html).append(‘<div id="deleteMe">Delete 5 Elements</div>’);
    $("#addMe").hide();
    });
    $(document).on(‘click’, ‘#deleteMe’, function() {
    for (var i = 1; i <= 5; i++) {
    $(‘#body’).find(‘p’).get(i).remove();
    }
    $("#addMe").hide();
    $("#deleteMe").hide();
    });

    });
    </script>
    </body>

    </html>
    [/html]

    Conclusion

    I hope you will like this article. Please share me your valuable thoughts and comments. Your feedback is always welcomed.

    Thanks in advance. Happy coding!

    Kindest Regards
    Sibeesh Venu

    Tags.remove()delete UIJQueryjquery eventsjquery functionsremove DOM
    Previous Article

    Ranked First In Monthly Top Members At ...

    Next Article

    C# Corner MVP 2014

    0
    Shares
    • 0
    • +
    • 0
    • 0
    • 0

    SibeeshVenu

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer.

    Related articles More from author

    • Create Custom Colour Palette
      HTML5JQuery

      Creating Custom Color Palette Using JQuery, HTML5, CSS

      March 29, 2015
      By SibeeshVenu
    • CodeProjectJQuery

      Find And Exclude Element From Array

      August 5, 2015
      By SibeeshVenu
    • CodeProjectJQueryJson

      Sort a JSON Array Programmatically by a Property

      June 10, 2015
      By SibeeshVenu
    • Hoisting In JavaScript
      CodeProjectJavaScriptJQuery

      Hoisting In JavaScript

      November 6, 2015
      By SibeeshVenu
    • JQuery

      Load Contents From Text File using jQuery

      March 29, 2015
      By SibeeshVenu
    • Code SnippetsJQuery

      Make Last Option As Selected In Select,Drop Down,Combo Box

      August 25, 2015
      By SibeeshVenu
    0

    My book

    Asp Net Core and Azure with Raspberry Pi Sibeesh Venu

    YouTube

    MICROSOFT MVP (2016-2022)

    profile for Sibeesh Venu - Microsoft MVP

    Recent Posts

    • React Native Android Release with Azure DevOps and Google Play Store
    • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment
    • Build, Deploy, Configure CI &CD Your Static Website in 5 mins
    • Easily move data from one COSMOS DB to another
    • .NET 8 New and Efficient Way to Check IP is in Given IP Range
    • Async Client IP safelist for Dot NET
    • Post Messages to Microsoft Teams Using Python
    • Get Azure Blob Storage Blob Metadata Using PowerShell
    • Deploy .net 6 App to Azure from Azure DevOps using Pipelines
    • Integrate Azure App Insights in 1 Minute to .Net6 Application

    Tags

    Achievements (35) Angular (14) Angular 5 (7) Angular JS (15) article (10) Article Of The Day (13) Asp.Net (14) Azure (65) Azure DevOps (10) Azure Function (10) Azure IoT (7) C# (17) c-sharp corner (13) Career Advice (11) chart (11) CSharp (7) CSS (7) CSS3 (6) HighChart (10) How To (9) HTML5 (10) HTML5 Chart (11) Interview (6) IoT (11) Javascript (10) JQuery (82) jquery functions (9) JQWidgets (15) JQX Grid (17) Json (7) Microsoft (8) MVC (20) MVP (9) MXChip (7) News (18) Office 365 (7) Products (10) SQL (20) SQL Server (15) Visual Studio (10) Visual Studio 2017 (7) VS2017 (7) Web API (12) Windows 10 (7) Wordpress (9)
    • .NET
    • Achievements
    • ADO.NET
    • Android
    • Angular
    • Arduino
    • Article Of The Day
    • ASP.NET
    • Asp.Net Core
    • Automobile
    • Awards
    • Azure
    • Azure CDN
    • azure devops
    • Blockchain
    • Blog
    • Browser
    • C-Sharp Corner
    • C#
    • Career Advice
    • Code Snippets
    • CodeProject
    • Cognitive Services
    • Cosmos DB
    • CSS
    • CSS3
    • Data Factory
    • Database
    • Docker
    • Drawings
    • Drill Down Chart
    • English
    • Excel Programming
    • Exporting
    • Facebook
    • Fun
    • Gadgets
    • GitHub
    • GoPro
    • High Map
    • HighChart
    • How to
    • HTML
    • HTML5
    • Ignite UI
    • IIS
    • Interview
    • IoT
    • JavaScript
    • JQuery
    • jQuery UI
    • JQWidgets
    • JQX Grid
    • Json
    • Knockout JS
    • Linux
    • Machine Learning
    • Malayalam
    • Malayalam Poems
    • MDX Query
    • Microsoft
    • Microsoft ADOMD
    • Microsoft MVP
    • Microsoft Office
    • Microsoft Technologies
    • Microsoft Windows
    • Microsoft Windows Server
    • Mobile
    • MongoDB
    • Monthly Winners
    • MVC
    • MVC Grid
    • MySQL
    • News
    • Node JS
    • npm
    • Number Conversions
    • October 2015
    • Office 365
    • Office Development
    • One Plus
    • Outlook
    • Page
    • PHP
    • Poems
    • PowerShell
    • Products
    • Q&A
    • Raspberry PI
    • React
    • SEO
    • SharePoint
    • Skype
    • Social Media
    • Software
    • Spire.Doc
    • Spire.PDF
    • Spire.XLS
    • SQL
    • SQL Server
    • SSAS
    • SSMS
    • Storage In HTML5
    • Stories
    • Third Party Software Apps
    • Tips
    • Tools
    • Translator Text
    • Uncategorized
    • Unit Testing
    • UWP
    • VB.Net
    • Videos
    • Virtual Machine
    • Visual Studio
    • Visual Studio 2017
    • Wamp Server
    • Web API
    • Web Platform Installer
    • Webinars
    • WebMatrix
    • Windows 10
    • Windows 7
    • Windows 8.1
    • Wordpress
    • Writing

    ABOUT ME

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer. If you would like to know more about me, you can read my story here.

    Contact Me

    • info@sibeeshpassion.com

    Pages

    • About
    • Search
    • Privacy Policy
    • About
    • Search
    • Privacy Policy
    © Copyright Sibeesh Passion 2014-2025. All Rights Reserved.
    Go to mobile version