Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript incrementing a variable
    text
    copied!<p>I'm trying to increment a variable in JavaScript by clicking on a link, it works the first time but then every other time if I click on it once it will show up 3 times, then 4, 5 and so on. A screenshot of the console: <img src="https://i.stack.imgur.com/cWa4M.png" alt="enter image description here"></p> <p>The numbers in the blue (right) are as expected, there is nothing wrong with this but as you can see on the left, each line is one click. the first log is fine but then it logs 1 three times, then 10 four times.</p> <p>Here is the code thats doing the work.</p> <pre><code>$('.here').live('click', function(eve) { eve.preventDefault(); var curpop = $(this).attr('rel') ++curpop $('.pop').html(curpop); console.log(curpop) }) </code></pre> <p><code>$(this).attr('rel')</code> is just a number (in the blue, right, in the picture) once you click the link it is meant to increment the number.</p> <p>Thanks for you help.</p> <p><strong>EDIT:</strong> I have set up the same thing in jsfiddle but it seems to work there, and all the suggestions still dont work on my project even though they should.</p> <pre><code>&lt;h4 class='left'&gt;Click &lt;a class="here" href="#" rel="1"&gt;here&lt;/a&gt;&lt;/h4&gt; &lt;h4 class='right'&gt; &lt;span class='pop'&gt; 1 &lt;/span&gt; &lt;/h4&gt; </code></pre> <p>This is the html, now there are multiple of these but are loaded via ajax, I am guessing that it would have something to do with the multiple instances of the class with no identifier to separate them?</p> <p><strong>More EDIT</strong> As I suspected the problem was with the "multiple instances of the class with no identifier to separate them" so I just made each one like this:</p> <pre><code>&lt;span id="&lt;?php echo $thereID; ?&gt;_span" class='pop'&gt; 1 &lt;/span&gt; </code></pre> <p>then</p> <p><code>$('#' + thereID + '_span').html(curpop);</code></p> <p>Thanks for all the help</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload