user2154508
user2154508

Reputation: 345

Count element clicks and define an max

I tried to count an element clicks, and, in the right number call some action.

var count = 0;
document.getElementById("rolarbaixo").onClick = function(e) {
     if( count >= 3 ) {
        var elem = document.getElementById("noticia");
            elem.setAttribute("style","top: 0px;");
     }
     else {
          count ++;
     }
};

When i clicked 3 times in the link "rolarbaixo" the div "noticia" set the "top: 0px;", but this doesn't work. Why?

Upvotes: 0

Views: 117

Answers (3)

adeneo
adeneo

Reputation: 318192

It's onclick in lowercase

var count = 0;
document.getElementById("rolarbaixo").onclick = function (e) {
    if (count >= 2) {
        var elem = document.getElementById("noticia");
        elem.style.top = "0px";
    } else {
        count++;
    }
};

FIDDLE

And it's >= 2 for three clicks (zero based and all).

AS the question is tagged jQuery, this would be it

$('#rolarbaixo').on('click', function() {
    var clicked = $(this).data('clicked') || 0;
    if (clicked >= 2) $('#noticia').css('top', 0);
    $(this).data('clicked', ++clicked);
});

FIDDLE

Upvotes: 1

JJJCoder
JJJCoder

Reputation: 16936

count ++ should be count++. If you press F12, you will be able to get to the developer tools and debug the javascript.

Upvotes: 1

Alex
Alex

Reputation: 11245

Misprint in else statement and change onclick to lowercase:

var count = 0;
document.getElementById("rolarbaixo").onclick = function(e) {
     if( count >= 3 ) {
        var elem = document.getElementById("noticia");
            elem.setAttribute("style","top: 0px;");
     } else {
          count++;
     }
};

Upvotes: 0

Related Questions