Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery live() ... have to click twice to activate links?

Tags:

jquery

click

live

I have the following bit of code, simply:

$(function() {
  $('a.add-photos-link').live('click', function(e) {
    $(this).colorbox({
      overlayClose: false,
      onComplete: function() {
        $('#add_photos').submit(function(e) {
          // more stuff to do
          e.preventDefault();
        });
      }
    });
    e.preventDefault();
  });
});

However, this only seems to work after single-clicking on the link TWICE. These links are dynamically added to the page (a.add-photos-link).

Why is this happening and what can I do to fix it so it fires after the first single-click?

like image 801
neezer Avatar asked Mar 14 '10 06:03

neezer


People also ask

Why do I have to click twice Javascript?

That's because you're registering an event listener on every click! So your listener executes once more every time you click.

How to trigger double click in jQuery?

The dblclick() is an inbuilt method in jQuery which is used to trigger the double-click event to occur. This method occurs when the selected element will be double clicked. Syntax: $(selector).

How to avoid double click on anchor tag?

$("a. button"). one("click", function() { $(this). click(function () { return false; }); });

How to detect double click in jQuery?

jQuery dblclick() Event The dblclick event occurs when an element is double-clicked. The dblclick() method triggers the dblclick event, or attaches a function to run when a dblclick event occurs.


1 Answers

Your current code only creates a colorbox for the link. It does not open the colorbox, which is why you need to click the link twice: once to create it and again to open it.

You can use the open option (as documented) when creating the colorbox to open it immediately, like so:

$(this).colorbox({
  open: true,
  overlayClose: false,
  onComplete: function() {
    // ...
  }
});
like image 130
brianpeiris Avatar answered Oct 14 '22 06:10

brianpeiris