Here's a simple pattern: var counter = $('element0').length; $('element1').text(counter); $('element2').click(function () { $('element1').text(counter--); });
Live example: open a live JavaScript console (like Firebug if you're using Firefox), then paste the following $('body').append('<div id="test"></div>'); $('#test').css({ position: 'absolute', top: '200px', right: '0px', width: '80px', padding: '10px', backgroundColor: '#f00', color: '#fff' }); var count = 25; $('#test').text(count); $('body').click(function () { $('#test').text(--count); }); Launch it, then click on the body: you'll see the number decrease :) On Thu, Jul 9, 2009 at 16:46, littlerobothead<nickmjo...@gmail.com> wrote: > > I'm sure this is going to be an easy one. I'm just not seeing a > solution in any of the usual places. > > I'm getting the number of elements in a table like so: > > var count = $("#dataset-b tr").length; > > Then appending that number to a span, like so: > > $(".count").html("(" + count + ")"); > > What I want to do is subtract one from that number every time a user > clicks a row in the table I'm generating, like so: > > $("#dataset-b tr").click(function(){ > $(this).fadeOut(300); > var int = 1; > var prod = count - int; > $(".count").html("(" + prod + ")"); > }); > > The problem is that it works on the first click (9 changes to 8), but > nothing happens on subsequent clicks. Again, I'm sure this is > something simple I'm missing. Thanks for any help. > -- Linux user #437712