7

I disabled the div by using following code

$("#menuDiv1").children().bind('click', function(){ 
     return false; 
});

menuDiv contains

<ul>
    <li><a class="home" href="#/Dashboard">Dashboard</a></li>
    <li><a class="forms" href="#/ViewLeads">Lead</a></li>
    <li><a class="forms"  href="#/ViewCases/0">Cases</a></li> 


</ul>

but now I want to enable it again. How can I do that?

And is there any other solution for the same in angular js?

1
  • 1
    You are using Angular, then try to achieve the result using anuglar Commented Jun 3, 2015 at 6:49

6 Answers 6

3

you can use unbind to remove disable click event

 $("#menuDiv1").children().unbind('click');

To re enable click event use below code.

  function clickfunc(){
     // your code
  }

  $("#menuDiv1").children().bind('click',clickfunc); 

Second option

you can use off to remove disable click event

$("#menuDiv1").children().off('click');

To re enable click event use below code.

  function clickfunc(){
     // your code
  }

  $("#menuDiv1").children().on('click',clickfunc); 

EDIT as discussed below code is avoid click event on a,button tags.

DEMO

function clickfunc(event){
     if(event.target.nodeName === 'A' || event.target.nodeName == "BUTTON"){
      return false;
     }

      // your code
     alert("click")
}

$("#menuDiv1").children().on('click',clickfunc); 
Sign up to request clarification or add additional context in comments.

5 Comments

there are no. of buttons and href tags that perform the different task on click . But if I use bind with clickfunc then it will perform same task for all . How can I resume the default operation of buttons and <a hrf> tags.
@SunilGarg you use of not() function like .children().not('button')
@SunilGarg here is perfect example for what you want it avoid buttons and href click and return false jsfiddle.net/cjbwh39q/1
The code you provided is working perfectly fine. But I need to restore my previous functionality of my div buttons. Please check question again I provided my div data
@SunilGarg can't understand .. means i have provide you many option to avoid click event on specific element . also i cant see button in side your HTML .. can you explain in detail ?? when you disable click event and when you want to enable click event ?
3

You can use unbind to disable and bind to enable again.

$("#menuDiv1").children().unbind('click');

To bind it again

$("#menuDiv1").children().bind('click', clickhander);

You better look to use jQuery.on and jQuery.off instead of bind and unbind.

Comments

1

In Angular you can use ng-click and ng-disabled in combination. For example:

<button ng-click="clicked()" ng-disabled="val">Click</button>

And in controller set the val to true or false to enable/disable click on the button.

$scope.val = true; //To disable button

EDIT:

For div you can do it like this:

 <div ng-click="val || clicked()" ng-disabled="val">Click</div>

4 Comments

ng-disabled is working fine with buttons. But when I use it with div nothing happens. I mean buttons or <a href > still work. Can we apply it on div or not ?
<div class="container" id="menuDiv1" ng-disabled="true"> . I used directly true for ng-disabled. Not working.
@SunilGarg then in the ng-click replace val with true. But I recommend you to use a variable for ng-disabled instead of tru so that you can modify it later.
I changed my div to <div ng-click="val || clicked()" ng-disabled="val" class="container" id="menuDiv1"> and in controller set val to true. but still not working.
0

using on/off you can enable or disable click event

Disable click

$("#menuDiv1").children().off('click');

Enable Click

$("#menuDiv1").children().on('click', function(){ 

// do here work

});

Comments

0

You would either unbind previously bound click event handler, or prevent event conditionally:

$("#menuDiv1").children().bind('click', function (e) {
    if ($(this).hasClass('no-click')) {
        e.stopPropagation();
    }
});

Btw, it's better to use e.stopPropagation(); explicitly to prevent child-to-parent event bubbling, rather than implicit return false.

Comments

0

Based on the new information in your question, the proper way to do this in AngularJS is to use the $locationChangeStart event.

Plunker

app.run(function ($rootScope, $location) {
  $rootScope.$on('$locationChangeStart', function (event, next, current) {
    if ($rootScope.menuDeactivated) {
      event.preventDefault();
    }
  });
});

This is a very simplistic example of that sets a variable on the $rootScope, but you could instead run a function (helpful if you want to save form values or prompt the user before changing your route for example) or inject a service (such as if you want to check if a user is logged in), etc.

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.