[Solved] Javascript – Looping and adding a class to an element [closed]


Here you go: http://jsfiddle.net/29Sby/1/

<style>
.clicked{color:green; font-weight:bold;}
</style>

<a id="link1" href="https://stackoverflow.com/questions/21706146/javascript:void(0); /*put URLs here if you need to*/" onclick="doStuff(this)">LINK 1</a><br/>
<a id="link2" href="javascript:void(0);" onclick="doStuff(this)">LINK 2</a><br/>
<a id="link3" href="javascript:void(0);" onclick="doStuff(this)">LINK 3</a><br/>
<a id="link4" href="javascript:void(0);" onclick="doStuff(this)">LINK 4</a><br/>
<a id="link5" href="javascript:void(0);" onclick="doStuff(this)">LINK 5</a><br/>

<script>
var counter = 0;

doStuff=function(obj)
{
    //alert(obj.id);
    if (obj.className === 'clicked') {
        //do nothing
        alert('already clicked');
    } else {
        obj.className="clicked";
        counter += 1;
    }

    if (counter === 3) {
        alert('call a function if you want');
    }
}
</script>

1

solved Javascript – Looping and adding a class to an element [closed]