Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8873401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:27:23+00:00 2026-06-14T18:27:23+00:00

I’m currently doing my first steps in jQuery and have a problem with .remove()

  • 0

I’m currently doing my first steps in jQuery and have a problem with .remove(). There are already a lot of questions from people with similar problems but they don’t help me.

In a HTML file, I have a form and the following div that serves as an alert box and displays whether the form validated correctly or not. In its original state, the div contains a single button for closing it.

<div id="my-form-alert-box">
    <button id="my-form-alert-button" type="button" class="close">x</button>
</div>

When the HTML page loads for the first time, the alert box should not be displayed. That’s why I add the following CSS:

<style type="text/css">
    #my-form-alert-box {display: none;}
</style>

When the form gets submitted and validated, I append <p>some text</p> to this div and then the alert box is displayed. When I close the alert box using the button, it disappears but the <p>some text</p> is not removed. Why is this the case?

Here is my jQuery code:

$(document).ready(function() {

    var $myFormAlertBox = $('#my-form-alert-box');
    var $myFormAlertBoxParagraph = $('#my-form-alert-box p');

    $('#my-form-alert-button').on('click', function(event) {

        $myFormAlertBoxParagraph.remove();
        $myFormAlertBox.hide();

    });

    $('#my-form').on('submit', function(event) {

        $.ajax({
            // ...
            success: function(data) {

                // The content of the alert box should be removed
                // and the alert box itself should be hidden also when
                // the form gets submitted again and not only when the
                // button is clicked
                $myFormAlertBoxParagraph.remove();
                $myFormAlertBox.hide();

                // data contains <p>some text</p>
                $myFormAlertBox.append(data).show(); 
            }

        });

        event.preventDefault();

    });        

});

Appending the data works fine, but removing it does not. Can you help me? Thank you very much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:27:24+00:00Added an answer on June 14, 2026 at 6:27 pm

    Your initial assignment of $myFormAlertBoxParagraph will fail as there isn’t a paragraph in your markup when it is called. Adding a ‘placeholder’ paragraph into your markup should fix it. This explains why the .remove() will fail.

    For your ajax, try something like this to keep the variables assigned with their new values:

    //...
    $.ajax({
        // ...
        success: function(data) {
            // Remove the existing paragraph.
            $myFormAlertBoxParagraph.remove();
    
            // This updates the paragraph object with the new one.
            $myFormAlertBoxParagraph = $(data);
    
            // Add the new paragraph and ensure the alert box is visible.
            $myFormAlertBox.append($myFormAlertBoxParagraph).show();
        }
    });
    //...
    

    This will remove the paragraph tag from the alert box, and add the new one. There’s no need to .hide() it and then immediately .show() it afterwards. However, adding the .show() after the .append() will cover you if it has been hidden by your click event.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I know there's a lot of other questions out there that deal with this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have been unable to fix a problem with Java Unicode and encoding. The
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.