Reputation: 2851
$('#likebtn1').click(function(){
$.post("postlike.php",
{img1link: img1}
);
$(this).click(false);
});
When the div likebtn1
is clicked, it needs post some data to a PHP file so that it can update a table. I only want it to be allowed to be clicked once (to prevent multiple clicks by 1 person). For some reason the above code isn't working. If I click lots of times, it still keeps updating the table.
Edit: When the user clicks another div the click needs to be re-enabled.
Upvotes: 0
Views: 70
Reputation: 24001
you can change pointer-events in css for this div
.addclass{
pointer-events: none;
}
$('#likebtn1').click(function(){
$.post("postlike.php",
{img1link: img1},
function(){
$(this).addClass('addclass');
}
);
});
in another div click event
$('#likebtn1').removeClass('addclass');
Upvotes: 0
Reputation: 243
If you want to do it more than once, using the on handler:
$('#likebtn1').click(function(){
$.post("postlike.php",
{img1link: img1}
);
$(this).off('click');
});
This has benefits over event binding, because off
accepts a callback:
$(this).off('click', function () {});
Upvotes: -1
Reputation: 1
var _clicked = false;
var _click = function(e) {
$.post("postlike.php",{img1link: img1});
_clicked = true;
};
$("#likebtn1").one("click", _click);
if (_clicked) {
$("#anotherDiv").on("click", _click);
};
Upvotes: 1
Reputation: 61092
I'd do it like so:
function myFunction() {
$('#likebtn1').one('click', function () {
$.post("postlike.php", {
img1link: img1
});
});
}
myFunction(); // run on document.ready
$('#myOtherButton').click(function() {
myFunction(); // re-enable the other button
});
Upvotes: 0
Reputation: 8291
Use $('#likebtn1').one(function(){})
UPDATE
You can also use $('#likebtn1').bind('click')
and $('#likebtn1').unbind('click')
events if you need to disable click
first and later enable it again.
Upvotes: 2