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

  • Home
  • SEARCH
  • 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 7162489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:44:36+00:00 2026-05-28T13:44:36+00:00

On clicking a link, a div is created which is draggable. I have another

  • 0

On clicking a link, a div is created which is draggable. I have another link which I want to be able to use to remove this newly created div.

Jquery code:

     var counter = 0;

 $(document).ready(function(){
 $("#alphalink").click(function(){ 

               var name = 'element'+ counter++;

                $('#elementarea').prepend('<div id="' + name + '"></div>');
               $("#" + name).prepend('<img id="theImg" src="http://us.metamath.org/symbols/alpha.png" />'); 
               $("#" + name).css("z-index", counter);  
                $("#" + name).css("position", "absolute");
                $("#" + name).draggable({ cursor: "move"});
               });


  $("#cleardiv").click(function(){ 
                 var olddiv= 'element'+ counter;
                 alert(olddiv);
                 $("#element" + counter).remove();
                      });

 });

HTML code:

       <div id="elementarea" style="width:300px; height:200px; background-color:#ccc; margin-top:10px; margin-right: 15px; float:left ">
         <p>Staging area here</p>
         </div>
        <div id ="alphalink">
    <p> Alpha Click </p>
     </div>


     <div id ="cleardiv">
     <p> clear item </p>
      </div>

On clicking on “Alpha Link”, a draggable div is created. On clicking “clear item”,the div should be removed.

However, the jquery .remove() does not seem to remove this newly created div. What is going wrong in this example?

You can try out the entire code here – http://jsbin.com/iboxoy/95/edit#source

  • 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-05-28T13:44:37+00:00Added an answer on May 28, 2026 at 1:44 pm

    The problem is here:

    var name = 'element'+ counter++;
    

    The postfix increment counter++ operator returns old value to be used as a result of expression, and than increments the variable, so you end up with div, with id 0, but counter value is 1. Try replacing with prefix increment ++counter.

    Also, it would be a good idea to keep those created divs in an array so you can always access all of them:

    var divs = [];
    //Inside 'alpha click' handler
        divs.push($("#" + name));
    
    //inside "cleardiv" handler
        var toRemove = divs.pop();
        toRemove.remove();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some elements like <a class='link'>click</a> which are created after clicking on another
I have a DIV with a link and a SPAN. When clicking the link,
I want to have java script clicking a link on the page..I found something
I have a page in which i have provided a link clicking on which
I have a timer in my JavaScript which needs to emulate clicking a link
I have this link: <a href=data:application/pdf;base64,JVBERi0x...KJSVFT0YK>PDF</a> In Safari on MacOsX, clicking the link instantly
I display the div by clicking the link, but now I want to hide
I have a link appended to a div element. And on clicking the link,
I'm trying to do this without Jquery. I want to show a div when
I've created a div which I want to fill with a different embedded video

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.