Search code examples
jquerybuttonconditional-statementshrefblock

jQuery blocking button on specific condition


I have created something like this to block the button when countdown has ended. The result

    if($(".stm_countdown div").hasClass(".countdown_ended")) {
     $('.vc_general').click(function(e) {
        e.preventDefault();
     });
    }

Question, why this code is not working?

https://jsfiddle.net/0dg2vL1z/3/

What am I missing ;-) ?

Thanks in advance, Tom


Solution

  • You have a couple of problems.

    First, you should bind your click event outside the condition. Then check for your condition inside the function.

    Second, you should look for hasClass("classname"), not hasClass(".classname").

    $('.vc_general').click(function(e) {
      if ($(".stm_countdown div").hasClass("countdown_ended")) {
        e.preventDefault();
        alert("Button is blocked");
        return false;
      }
    });
    

    Link to updated fiddle.