jQuery bind unbind click event -


i trying jquery calculate amount of times id has been clicked. instance if #kwick_1 clicked once, want load video 'else' nothing.

and looking @ applying throughout function same goes 2, 3, 4 etc.

how achieve this?

var timesclicked = 0;   $('#kwick_1, #kwick_2, #kwick_3, #kwick_4, #kwick_5, #kwick_6, #kwick_7').bind('click', function(event){    var video = $(this).find('a').attr('href'); /* bunny.mp4 */     var clickedid = $(this).attr('id'); /* kwick_1 */    var vid = 'vid';    timesclicked++;     $(this).removeattr('id').removeattr('class').attr('id',clickedid + vid).attr('class',clickedid + vid);    if(timesclicked == 1) {      $.get("video.php", { video: video, poster: 'bunny.jpg', }, function(data){      $('.'+clickedid+vid).html(data);     });      } else {     /* nothing */    }    return false;   }); 

you can use .one() bit easier, this:

$('#kwick_1, #kwick_2, #kwick_3, #kwick_4, #kwick_5, #kwick_6, #kwick_7').one('click', function(event){   var video = $(this).find('a').attr('href'), clicked = this.id + 'vid';    $(this).attr({ id: clicked, 'class': clicked });   $.get("video.php", { video: video, poster: 'bunny.jpg', }, function(data){     $('.'+clicked).html(data);   });  }).click(function() { return false; }); 

the rest optimization , cutting down on repeated code, idea bind part want happen once using .one(), handler remove after first execution. we're binding return false using .click() since 1 should always happen. make sure bound after example above, since event handlers run in order bound.

also replace #kwick_1, #kwick_2.... selector .kwick if give them class, above still work , easier maintain.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -