Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow just one click in class JQUERY

I have a link that will load via ajax some content.

My problem is, I don't want to remove the text "Load comments", I just want to not allow more clicks in this class.

<a href="javascript:;" class="showcomments" id="'.$idf.'">Load comments</a>

Jquery

var Progressajax = false;
$(function() {
     $(".showcomments").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('showcomments');
    }); 
    });
        

I just want to see OK the first time. How can I remove this class?

$(element).removeClass('showcomments');

This is not working...

http://jsfiddle.net/qsn1tuk1/

like image 827
RGS Avatar asked Dec 07 '25 07:12

RGS


1 Answers

Use jQuery's one() function

$(".showcomments").one("click", function() {

http://www.w3schools.com/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.

like image 125
Richard Hamilton Avatar answered Dec 08 '25 19:12

Richard Hamilton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!