JQuery – Click event firing more than once


I had to use jQuery for a project.

I attached a click event to an element


$('#test a').click(function()
{
  $(this).children('img').attr("src","images/open.gif");

});

The problem i faced was that the click event was called more than once.

After a long search,got a solution and may this help one of u too.

.unbind did the trick 🙂


$('#test a').unbind("click").click(function()
{
  $(this).children('img').attr("src","images/open.gif");

});

Advertisements

8 thoughts on “JQuery – Click event firing more than once

  1. Also you can namespace your events to prevent undesired unbindings.

    $(‘#test a’).bind(‘click.myclick1’, function(e) { …..} );

    $(‘#test a’).bind(‘click.myclick2’, function(e){….});

    and then unbind just “myclick1”:

    $(‘#test a’).unbind(‘click.myclick1’);

  2. Thank you so much. i got a similar issue with jqgrid. i bind the click handler when gridcomplete. The gridComplete event is trigged when new row is added/deleted. so the handler is binded more than one.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s