0

Could you please advise how to disable /enable the click function using jquery api. Based on the role i have show the sports page to the user.

Using below bootstrap code for UI.

<div id="viewtest" class="btn-group btn-group-sm">
    <button id="C" class="btn btn-default" title="Cricket">Cricket</button>
    <button id="B" class="btn btn-default" autofocus="autofocus" autofocus title="BaseBall">BaseBall</button>
    <button id="T" class="btn btn-default disabled" title="Tennis">Tennis</button>
    <button id="G" class="btn btn-default disabled " title="Monthly">Golf</button>
</div>

I am using below function to handle the above buttons click events.

$(".btn-group > .btn").click(function () {
    $(this).addClass("btn-primary").siblings().removeClass("btn-primary");
    viewtype = this.id;
    $(this).blur();
    showPage();
});

For some criteria , i am disabling the click event using below function

var disableOptionsTabs=["C","T"];
        disableButtons(disableOptionsTabs);

function disableButtons(mybuttons) {
    $.each(mybuttons, function (key, value) {
        $("#" + value).off();
    });
}

Please advise how to enable the click event? or how to bind the click function for button.

I am using below method ,bind the click event for the specify buttons based on given advise . but it's not working.

 var enableOptionsTabs=["C","T"];
            enableButtons(enableOptionsTabs);
    function enableButtons(mybuttons){
           $.each( mybuttons, function( key,value ) 
           { $( "#" + value ).on("click",myOnClick);  }
          );

    }

2 Answers 2

2

If you are going to continually remove and add the event I would move the click functionality to a function you can reuse.

function myOnClick () {
    $(this).addClass("btn-primary").siblings().removeClass("btn-primary");
    viewtype = this.id;
    $(this).blur();
    showPage();
}

Then you can attach it like so:

$(".btn-group > .btn").on('click', myOnClick);

And remove it like so:

$(".btn-group > .btn").off('click');

As for the disabling loop if mybuttons is a list of elements you can just do this:

function disableButtons(mybuttons) {
    $.each(mybuttons, function () {
        $(this).off('click');
    });
}

You can because of how jQuery calls the function you pass it to each. It sets the context of that function for every element so you can just reference it using this.

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for your suggestion, can we do without function ?
Sure you can continually use an anonymous function if you want.
Do we need pass the current object ie this() to myOnclick function ?
this is the context of the function, you don't need to pass it. It's just there and in your case references the element that was clicked.
Using your suggestion ,we can disable the click event for the all button and vice versa,but my requirement is enable/disable the event based on the conditions, so that i am using off method in the loop.
|
0

I got answer from the below link ,

Best way to disable button in Twitter's Bootstrap

function disableButtons(mybuttons){
             $.each( mybuttons, function( key,value ) 
      {         $("#" + value).prop('disabled', true)     }
      );

    }
    function enableButtons(mybuttons){
       $.each( mybuttons, function( key,value ) 
       { 

       $("#" + value).prop('disabled', false);
       $("#" + value).removeClass("disabled");

       }
      );

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.