How would I disable all links with the button
class after they are clicked once? I\'d like to be able to do this in one place, and not have to change all of the
Yeah, set a value to keep track of this:
$("a").click(function (){
if( $(this).data("clicked") ){
return false;
}
$(this).data("clicked", true);
return true;
});
This works for both IE and Chrome with href and onclick attributes on anchor tags:
$(document).ready(
function()
{
$('a.button').on("click",
function(event)
{
$('a.button').prop('onclick',null);
$('a.button').removeAttr('href');
});
});
I like the idea of solution provided by Adam, however this didn't work for me. If you're having trouble using .one()
to disable links after click, I recommend trying the following.
$("a.button").bind("click", function( event ) {
// Unbind the click event from anchors with class button
$(this).unbind( event );
});
This is what I would try:
$("a.button").one("click", function() {
$(this).click(function () { return false; });
});
Bind all the links with class "button". Run the anonymous function only once (hence "one"), which rebinds the link to return false.
If you want to keep it looking more like what you have, try this:
$("a.button").click(function() { $(this).attr("disabled", "disabled"); });
$(document).click(function(evt) {
if ($(evt.target).is("a[disabled]"))
return false;
});
Events are only bound on document.ready, you should handle this with a .live event:
$("a.button").live("click", function() { $(this).attr("disabled", "disabled"); });
$("a[disabled]").live("click", function() { return false; });
This should work when you are using a function
var clickRunning = false;
function OpenSth(){
if (clickRunning) return;
clickRunning = true;
yourFunc({
/* do your stuff*/
},500, function () {
clickRunning= false;
});
}