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 9245641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:18:38+00:00 2026-06-18T09:18:38+00:00

I’m only going to post the code that’s relevant. If you need my whole

  • 0

I’m only going to post the code that’s relevant. If you need my whole code, let me know.

Anyway, I have the following code:

 var deck = [];
 var c = 0;
$('#add').click(function(){
     var addToDeck = $('input[name=searchItem]').val();
     addToDeck = addToDeck.toLowerCase();
     deck.push(addToDeck);
   $('#save').append('<div id="cardList' + (c++) + '">' + database[deck[deck.length-1]].name + '</div>');
 });

Which dynamically adds divs with unique id’s each time a certain button is pressed (ex: cardList1, cardList2, etc). Now, if the user clicks on some div with a particular id (say cardList2), I want to remove it from the list.

I tried something like this:

  $(document).on('click','#save', function(){
   $('#cardList' + 'c').remove();
  });

But it doesn’t work. I think I’m in the right direction though.

Just to re-iterate, this is a card game deck building application… so I want to make it easier on a user by letting them remove a card that’s already in their deck just by clicking on the div with the card id.

  • 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-18T09:18:39+00:00Added an answer on June 18, 2026 at 9:18 am

    In your second code example, c is not defined or does not have the value you expect. This should work though:

    $('#save').on('click','[id^=cardList]', function(){
       $(this).remove();
    });
    

    It will listen to click events on all elements having an ID which starts with cardList and which are descendants of the element #save (instead of listening to clicks on #save). It makes simpler because this will just refer to the element that was clicked and you want to remove.

    You could also give the elements an additional class and use it instead of the attribute-starts-with selector:

    // for example
    $('<div />', {
        id: 'cardList' + (c++), 
        'class': 'myclass', 
        text: database[deck[deck.length-1]].name
    }).appendTo('#save');
    
    // somewhere else
    
    $('#save').on('click','.myclass', function(){
       $(this).remove();
    });
    

    You should also think about whether you need unique IDs at all.

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

Sidebar

Related Questions

Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

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.