Some details are missing for this to work:
method .off()
is misconfigured:
The method can be used without parameters but it will remove all the event handlers added with .on()
, half dangerous / unpredictable. It is best to define which type of event (and not the event object itself as you have), and now to be safe to pass the function that you want to undo. That is:
$(this).off("click", eventCon);
// ou somente $(this).off(); que desaconselho
Another problem already mentioned is the issue of the code being run before the DOM is ready. This solves wrapper $("#div3").on("click", eventCon);
with a jQuery domready function. Example:
$(function () {
// código a correr aqui...
});
So the whole code would be:
$(function () {
$("#div3").on("click", eventCon);
});
var qtdadeCliques = 0;
var eventCon = function (evento) {
qtdadeCliques++;
if (qtdadeCliques >= 3) {
alert('Chegou a 3!');
$(this).off('click', eventCon);
}
};
jsFiddle: link