function inadvertently executed when using .live in jQuery
-
24-10-2019 - |
Question
I'm trying to bind a click event handler to some elements that are being created dynamically. But the function already gets executed on simply loading the page. I also tried the livequery plugin and .delegate which also had that unwanted habit.
$(".pika_thumb").live("click" ,( function () {
$("#video").hide();
$(".pika_main").show();
}));
How do I prevent my function to be executed on other events than a click on the specified elements?
Solution
The only obvious error is the brackets from around the function() { }
definition.
However that wouldn't cause immediate invocation unless you had trailing a ()
parameter list too. Is your code snippet complete?
OTHER TIPS
the syntax:
$(".pika_thumb").live("click" , function () {
$("#video").hide();
$(".pika_main").show();
});
check if you don't have something else that fires the click on your .pika_thumb class.
Aside from the brackets, my final working code is:
$("#gallery").delegate(".pika_thumb", "mousedown" , function () {
$(".video-js").get(0).pause();
$("#video").hide();
$(".pika_main").show();
});
Thanks everyone!