2

I just created mvc 4 application. Here the view of a table of that project.

I want to disable clickable function currently active button

For example:

disable click function of first row Active button , enable click function for Inactive button

and

disable click function of 2nd row Inactive button , enable click function for Active button

likewise rest of it.

enter image description here

Here the current cshtml code for this table view button function

<div class="btn-group btn-toggle" id="btn-toggle"> 


     @if (item.Status == true)
     {
<button class="btn btn-xs active btn-primary" data-HEI_ID = "@item.HEI_ID" data-status = "true" >Active</button>                       
<button class="btn btn-xs inactiveColor btn-default" data-HEI_ID = "@item.HEI_ID" data-status = "false" >Inactive</button>

    }

    else
   {
<button class="btn btn-xs btn-default" data-HEI_ID = "@item.HEI_ID" data-status = "true" >Active</button>                       
<button class="btn btn-xs inactiveColor btn-primary active" data-HEI_ID = "@item.HEI_ID" data-status = "false" >Inactive</button>

  }                        

  </div>

This is JavaScript code snippet to handle this buttons

   $('.btn-toggle').click(function () {

        $(this).find('.btn').toggleClass('active');
        //if ($(this).find('btn-primary').toggleClass('active')) {
        //$(this).prop('disabled', true);

        //}
        if ($(this).find('.btn-primary').size() > 0) {
            $(this).find('.btn').toggleClass('btn-primary');

        }
        if ($(this).find('.btn-danger').size() > 0) {
            $(this).find('.btn').toggleClass('btn-danger');

        }
        if ($(this).find('.btn-success').size() > 0) {
            $(this).find('.btn').toggleClass('btn-success');

        }
        if ($(this).find('.btn-info').size() > 0) {
            $(this).find('.btn').toggleClass('btn-info');

        }

        $(this).find('.btn').toggleClass('btn-default');

});
4
  • So what is your actual question. Is something not working? Commented May 15, 2015 at 9:09
  • currently it stay as both active/inactive buttons clickable, I want to disable active one Commented May 15, 2015 at 9:11
  • Do you mean something like this fiddle? Commented May 15, 2015 at 9:38
  • exactly , thats What I want Commented May 15, 2015 at 9:48

3 Answers 3

1

you can use :not():

$('.btn-toggle').find('button:not(.active)').click(function () {

The above one is binding a click event on the buttons which does not have .active css class in the parent div .btn-toggle

Where i am assuming* that you have .active css class in the css which turns the button in green background color.

*:You can replace it with your active class.

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

1 Comment

now active button disabled , your solution 50% right but to see actual button status I should refresh page , previously it showed without page refresh when I switch button state,
1

Wrap your button groups in a container to make it easier to use relative selectors

<div>
    <button class="inactive">Active</button>
    <button disabled="disabled">InActive</button>
</div>

and then use the following script

$('button').click(function() {
    if (!$(this).hasClass('inactive')) {
        return;
    }
    $(this).closest('div').children('button').toggleClass('inactive');
    $(this).prop('disabled', true);
    $(this).siblings('button').prop('disabled', false);
});

Refer fiddle

1 Comment

Do I assume that "exactly , thats What I want" is not really what you wanted after all?
0

you need to remove click event from active buttons on DOM load event

$(document).ready(function(){
  $('.active').unbind('click');
}

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.