0

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?

henning
  • 3
  • 1
  • 1
    The parentheses around the function are unneeded but shouldn't cause the issue described. I would imagine there's something else on your page causing this problem. Try placing an alert inside this click function and see if it is displayed on load. – James Montagne Apr 27 '11 at 13:57
  • I had an alert inside it and did fire. – henning Apr 28 '11 at 08:49
  • The parentheses seemed to actually cause the function to execute. I should have looked more closely to the example code. – henning Apr 28 '11 at 08:59
  • Very strange. Which browser were you testing it in? I had tried it in chrome just to see and it didn't fire. – James Montagne Apr 28 '11 at 15:41
  • I was testing with Firefox 3.6.16, Chromium 10.0.630.0 and Opera prior to 11.10. – henning Apr 29 '11 at 08:16

3 Answers3

0

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?

Alnitak
  • 334,560
  • 70
  • 407
  • 495
0

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.

alexl
  • 6,841
  • 3
  • 24
  • 29
0

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!

henning
  • 3
  • 1