I have a link that will load via ajax some content.
My problem is, I don't want to remove the text "Load ments", I just want to not allow more clicks in this class.
<a href="javascript:;" class="showments" id="'.$idf.'">Load ments</a>
Jquery
var Progressajax = false;
$(function() {
$(".showments").click(function(){
if(Progressajax) return;
Progressajax = true;
var element = $(this);
var id = element.attr("id");
Progressajax = false;
alert("ok");
$(data).hide().prependTo('.varload'+id).fadeIn(1000);
//$(element).remove();
$(element).removeAttr("href");
$(element).removeClass('showments');
});
});
I just want to see OK the first time. How can I remove this class?
$(element).removeClass('showments');
This is not working...
/
I have a link that will load via ajax some content.
My problem is, I don't want to remove the text "Load ments", I just want to not allow more clicks in this class.
<a href="javascript:;" class="showments" id="'.$idf.'">Load ments</a>
Jquery
var Progressajax = false;
$(function() {
$(".showments").click(function(){
if(Progressajax) return;
Progressajax = true;
var element = $(this);
var id = element.attr("id");
Progressajax = false;
alert("ok");
$(data).hide().prependTo('.varload'+id).fadeIn(1000);
//$(element).remove();
$(element).removeAttr("href");
$(element).removeClass('showments');
});
});
I just want to see OK the first time. How can I remove this class?
$(element).removeClass('showments');
This is not working...
http://jsfiddle/qsn1tuk1/
Share Improve this question edited Feb 16, 2022 at 14:28 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 16, 2015 at 18:26 RGSRGS 4,2635 gold badges40 silver badges74 bronze badges 2-
$('.showments')
will find all elements that have that class, and attach click handler to them. if you later remove the class, that doesn't do ANYTHING to the click handler. you'd have to remove the click handler itself, or have the handler check if the element still has the appropriate classs – Marc B Commented Sep 16, 2015 at 18:31 -
It sounds like you want to use
$.off()
which will turn off the listener on a class. Here is the doc on it. – area28 Commented Sep 16, 2015 at 18:32
2 Answers
Reset to default 8Use jQuery's one()
function
$(".showments").one("click", function() {
http://www.w3schools./jquery/event_one.asp
The one() method attaches one or more event handlers for the selected elements, and specifies a function to run when the event occurs.
When using the one() method, the event handler function is only run ONCE for each element.
When you bind an event handler, you bind to the element, not to the class. Removing a class from an element doesn't change which event handlers are bound to the element.
You could use off()
to remove the event handler:
$(this).off('click');
http://jsfiddle/om6ggvyu/