[ACCEPTED]-Increasing a number by 1 using jquery?-jquery

Accepted answer
Score: 12
var num = parseInt($.trim($(this).html()));
$(this).html(++num)

0

Score: 6

You want to take a look at .html() or .text(). Here is 1 an example:

$(this).text(function(i, t) {
    return Number(t) + 1;
});
Score: 3

HTML:

<span id="counter">0</span>

jQuery:

$('#counter').text(Number($('#counter').text())+1);

You can increase the counter 1 when clicking an existing button like this:

$(document).on('click','#your-button', function(){
  $('#counter').text(Number($('#counter').text())+1);
});
Score: 1

Just use a plugin.

(function($) {
    $.extend($.fn, {
         "addOne": function() {
              var num = parseInt(this.text(), 10);
              this.text(++num);
         },
         "subtractOne": function() {
              var num = parseInt(this.text(), 10);
              this.text(--num);
         }
    });
}(jQuery))

Then call

$(".fav").live("click", function(e) {
     $(this).toggleClass("highlight").addOne();
});

0

More Related questions