Add a number to class and update on each click using jQuery

I have a simple anchor element with a class applied. Each time the anchor is clicked I would like to add a number to the class using jQuery, each additional click should increase this number by 1.

Default Example:

<a id="idname" class="classname" href="#">Next</a>

1st Click:

<a id="idname" class="classname1" href="#">Next</a>

2nd Click:

<a id="idname" class="classname2" href="#">Next</a>

And so on...

Thanks in advance for any tips or suggestions


Try this

var className = "classname", count = 1;
  this.className = (className + (++count));

var clickCount = 0;
$("#idname").click(function(event) {
    $(this).removeClass(clickCount > 0 ? "classname" + clickCount : "className");
    $(this).addClass("classname" + clickCount); 

This should do the job:

var count = 0;
$("#idname").click(function() {
    $("#idname").attr('class', 'classname' + count) ;

Try this

    $(document).ready(function() {
        className = $('#idname').attr('class');
        $('#idname').click(function() {
            $this = $(this);
            var counter = $this.attr('class').split(className)[1];              
            var nextNum = 1;
            if (counter != '') //if the counter has already started
                nextNum = parseInt(counter) + 1;
            $this.removeClass().addClass(className + nextNum);

Need Your Help

How to get array of bits in a structure?

c++ c internals

I was pondering (and therefore am looking for a way to learn this, and not a better solution) if it is possible to get an array of bits in a structure.

Is this efficient use of the is and as operator in a DrawableGameComponent?

c# menu xna

I'm learning to use the as operator, and my goal was to create an option window (non windows form) that can: