Some details are missing for this to work:
the method .off()
is poorly configured:
The method can be used without parameters but then will remove all the added handlers Event with the .on()
, dangerous/unpredictable environment. The best is to decide what type of event (and not the event object itself as you had), and by the way to be safe to pass the function you want to cancel. That is to say:
$(this).off("click", eventCon);
// ou somente $(this).off(); que desaconselho
Another problem already mentioned is the question of the code being run before the DOM is ready. This solves the involvement $("#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);
}
};
What’s your question? Here, this code is working normal...
– Felipe Avelar
The strange thing is that when I put this error on the website: Uncaught Typeerror: Undefined is not a Function. @Felipeavelar
– Weliton Figueiredo
try to put inside this code block: (Function( $ ) { $(Function() { // Your code with dolar here }); })(jQuery);
– Gabriel Rodrigues
What version of jQuery is the site using? Before you check this put your code within $(Function(){ /HERE/ })
– William Lima
It worked @Gabrielrodrigues.
– Weliton Figueiredo
the solution was taken from here: http://answall.com/questions/769/howto avoid conflict-entre-jquery-e-mootools
– Gabriel Rodrigues
If the
#div3
already exist on the page at the time you call theon
, also needs to correct the call tooff
. You must passeventCon
instead ofevento
(as in the response of Paul below).– bfavaretto