How can I increment and decrement a counter for the amount of cards in a deck with a certain card name?
<p>Good day;</p>

<p>I'm continuing my attempt to create a deck building application for Cardfight!! Vanguard</p>

<p>There is a rule you can't have more than 4 of the same card in a deck, and no more than 50 cards in a deck.</p>

<p>I'm trying to make it so that I have a counter that increases when a card is added to a deck and decreases when a card is taken out of the deck. But this counter must be for each individual card which starts off at 0 and increases by 1 every time a card with a specific name is added to the deck.</p>

<p>I have a vanguard object and database array where I can create new Vanguard objects easily. I have a variable within the Vanguard object (<code>this.cardLimit = 0</code>) which will set the card limit initially to 0 for each new card.</p>

<p>Then when a new card <code>[addToDeck]</code> is added to the deck, it will increment a counter for that specific card within the database: <code>database[addToDeck].cardLimit++</code>, until the maximum of 4 is reached, after which point this if statement will not work:</p>

<pre><code>if (deck.length &lt; 51 &amp;&amp; database[addToDeck].cardLimit &lt; 5)
</code></pre>

<p>My problem is - as you will see in the code below - I can't figure out a way to decrement this value when I remove a card using jquery. What I do is dynamically add card names to a div, and when a card name in that div is clicked on, it will remove not only the card from the div but the card from the deck array as well by splicing.</p>

<p>I can't just do <code>database[addToDeck].cardLimit--</code> as you will see in my code because I use jQuery and "this" to specifically refer to the part of the <code>div</code> being clicked on.</p>

<p>My question is then.... how can I decrease the counter based on the specifications of the code I have provided so far? When my Jquery (last jQuery instance in the code) for removing the part of the deck runs, I need to decrease the counter for the specific card that was removed.</p>

<p>Let me know if you need any further clarification... It's not easy for me to explain my problem in simple terms. Thanks so much for reading.</p>

<p>JavaScript / Jquery: </p>

<pre><code>function Vanguard(name,grade,skill,power,shield,critical, type, trigger, nation, clan, race, flavor, effect, imageURL){
    this.name = name;
    this.grade = grade;
    this.skill = skill;
    this.power = power;
    this.shield = shield;
    this.critical = critical;
    this.type = type;
    this.trigger = trigger;
    this.nation = nation;
    this.clan = clan;
    this.race = race;
    this.flavor = flavor;
    this.effect = effect;
    this.imageURL = imageURL;
    this.cardLimit = 0;
};

var database = {};
database['asura kaiser'] = new Vanguard("Asura Kaiser", 3, "Twin Drive!!", 11000, "", 1, "Normal Unit", "None", "Star Gate", "Nova Grappler", "Battleroid", "The death blow! Kaiser Buster!", "&lt;br/&gt;[CONT](VC/RC):If you do not have another «Nova Grappler» vanguard or rear-guard, this unit gets [Power] -2000. &lt;br/&gt; [AUTO](VC):When this unit's drive check reveals a grade 3 «Nova Grappler», choose one of your rear-guards, and [Stand] it.", "http://images4.wikia.nocookie.net/__cb20120428001646/cardfight/images/thumb/a/a6/VGE_BT01-008EN.jpg/300px-VGE_BT01-008EN.jpg");
database['king of knights, alfred'] = new Vanguard("King of Knights, Alfred", 3, "Twin Drive!!", 10000, "", 1, "Normal Unit", "None", "United Sanctuary", "Royal Paladin", "Human", "I command you under the name of the King of Knights! Warriors, heed my call!", "&lt;br/&gt;[CONT](VC):Your units cannot boost this unit. &lt;br/&gt; [CONT](VC):During your turn, this unit gets [Power]+2000 for each of your «Royal Paladin» rear-guards. &lt;br/&gt; [ACT](VC/RC):[Counter Blast (3)] Search your deck for up to one grade 2 or less «Royal Paladin», call it to (RC), and shuffle your deck.", "http://images2.wikia.nocookie.net/__cb20121009013434/cardfight/images/thumb/9/95/BT01-001EN_RRR.jpg/300px-BT01-001EN_RRR.jpg");
database['dragonic overlord'] = new Vanguard("Dragonic Overlord", 3, "Twin Drive!!", 11000, "", 1, "Normal Unit", "None", "Dragon Sanctuary", "Kagerou", "Dragon", "&lt;br/&gt;(TD02):Reduce all to ashes, flame of the apocalypse! Eternal Flame! &lt;br/&gt; (BT01):Burn, flame of despair! Eternal Flame!", "&lt;br/&gt;[CONT](VC/RC):If you do not have another «Kagero» vanguard or rear-guard, this unit gets [Power]-2000. &lt;br/&gt; [ACT](VC/RC):[Counter Blast (3)] Until end of turn, this unit gets [Power]+5000, gets '[AUTO](VC/RC):When this unit's attack hits an opponent's rear-guard, [Stand] this unit', and loses 'Twin Drive!!'.", "http://images1.wikia.nocookie.net/__cb20120131161306/cardfight/images/thumb/3/33/TD02-001EN.jpg/300px-TD02-001EN.jpg");
database['ceo amaterasu'] = new Vanguard("CEO Amaterasu", 3, "Twin Drive", 10000, "", 1, "Normal Unit", "None", "United Sanctuary", "Oracle Think Tank", "Human", "&lt;br/&gt;(BT01):Shape my desire. Project it before me, Yata no Kagami! &lt;br/&gt;(EB05):Feel the flow of time. You will create the future.", "&lt;br/&gt;[CONT](VC):During your turn, if the number of cards in your hand is four or greater, this unit gets [Power] +4000. &lt;br/&gt; [AUTO](VC):At the beginning of your main phase, Soul Charge (1), look at the top card of your deck, and put that card on the top or the bottom of your deck. &lt;br/&gt; [AUTO](VC/RC):[Soul Blast (8) &amp; Counter Blast (5)] When this unit's attack hits, you may pay the cost. If you do, draw up to five cards.)", "http://images2.wikia.nocookie.net/__cb20120831173054/cardfight/images/thumb/3/30/BT01-S05EN_SP.jpg/300px-BT01-S05EN_SP.jpg");
database['alfred early'] = new Vanguard("Alfred Early", 3, "Twin Drive!!", 10000, "", 1, "Normal Unit", "None", "United Sanctuary", "Royal Paladin", "Human", "To preserve the smiles of his people, the young King leads knights to battle.", "AUTO: When this unit is placed on Vanguard Circle, choose up to one card named 'Blaster Blade' from your soul, and call it to Rear-guard Circle.", "http://images1.wikia.nocookie.net/__cb20120926194218/cardfight/images/thumb/5/5e/PR-0005EN.jpg/300px-PR-0005EN.jpg");

function printVanguard(p, name){
      for (var p in database[name]){
       if (p !== 'imageURL'){
         document.getElementById('output').innerHTML +=('&lt;b&gt;' + p.charAt(0).toUpperCase() + p.slice(1) + '&lt;/b&gt;: ' + database[name][p] + '&lt;br /&gt;');
      }}
};

$(document).ready(function() {
 $('#button').click(function(){
   $('#output').html(""); //clear old search
   $('#image').html(""); // clear old image
    var userInput = $('input[name=searchItem]').val();
    userInput = userInput.toLowerCase();
     $('#output').append(printVanguard(userInput, userInput));
   var image = $('&lt;img&gt;').attr('src', database[userInput].imageURL);
   $('#image').append(image);
 });
 var deck = [];

 $('#add').click(function(){
     var addToDeck = $('input[name=searchItem]').val();
     addToDeck = addToDeck.toLowerCase();
     database[addToDeck].cardLimit++;
     if (deck.length &lt; 51 &amp;&amp; database[addToDeck].cardLimit &lt; 5){
      deck.push(addToDeck);
      $('#quantity').html("");
      $('#save').prepend('&lt;div id="quantity"&gt;' + 'Total: ' + deck.length + '&lt;/div&gt;');   
      $('#save').append('&lt;div id="cardList"&gt;' + database[deck[deck.length-1]].name + '&lt;/div&gt;');
      }
 });
  $(document).on('click','[id^=cardList]', function(){
   $(this).remove();
     deck.splice(deck[this], 1);
    $('#quantity').html("");
     $('#save').prepend('&lt;div id="quantity"&gt;' + 'Total: ' + deck.length + '&lt;/div&gt;');
  });
});
</code></pre>

<p>HTML code:</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;link rel="stylesheet" type="text/css" href="style.css" /&gt;
        &lt;script type = "text/javascript" src='script.js'&gt;
        &lt;/script&gt;
        &lt;title&gt;&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
     &lt;form name="searchForm"&gt;
         &lt;input type="text" id="search" placeholder="Enter Card Name" name="searchItem"/&gt;
     &lt;/form&gt;
     &lt;div id="button"&gt;Search!&lt;/div&gt;
     &lt;div id="add"&gt;Add to Deck&lt;/div&gt;
    &lt;br/&gt;
    &lt;div id="output"&gt;&lt;/div&gt;
    &lt;div id="image"&gt;&lt;/div&gt;
    &lt;div id="save"&gt;&lt;/div&gt;
    &lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>Fiddle: jsfiddle.net/8QR7q
Provided by Malik
<br><br>
Test Inputs:  "Alfred Early", "CEO Amaterasu", "Dragonic Overlord", "Asura Kaiser", or "King of Knights, Alfred"</p>
