1

I have written the following line to add an disable attribute to an element in angular

angular.element(document.getElementsByClassName("pit")).setAttribute('disabled');

but it is not working and throwing an error:

angular.element(...).setAttribute is not a function

2
  • You should definitely use ng-disabled if you are using AngularJS. Commented Jan 20, 2017 at 12:46
  • yeah i too was thinking that but that element is dynamically created Commented Jan 20, 2017 at 12:48

5 Answers 5

3

angular.element returns a jQuery/jQuery lite wrapped element, not the raw DOM element.

You can set it using jQuery methods:

angular.element(document.getElementsByClassName("pit")).attr('disabled', true);

This is not really the angular way though. Consider adding a service to provide values to ng-disabled, or directive to manage the disabled state.

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

4 Comments

the element is dynamically created how can i use ng-disabled on it?
You can use $compile to create dynamic elements which are still angular-able. Related question: stackoverflow.com/questions/15279244/…
i am trying to create frost screen effect whenever i click on a particular button all the contents should get blurred for that i have applied blur class on them(CSS) and I am then trying to disable thier functionalities in which case i am disabling them. so can u suggest me some other way to do so...
update your question if a jsfiddlerso we may help you.
2

angular.element returns a jQuery or jqLite Object, both has an attr() function that you can use instead, so:

instead of:

angular.element(document.getElementsByClassName("pit")).setAttribute('disabled');

you should write:

angular.element(".pit").attr('disabled', 'disabled');

Comments

1

Angular has built in jQuery, so you could just add

$(".pit").attr('disabled', true);

Comments

0
angular.element(document.querySelector(".pit")).setAttribute('disabled',true);

1 Comment

Although this code might solve the problem, one should always consider adding an explanation to it.
0

This statement will work only if the element is an input type

angular.element(document.getElementsByClassName("pit")).attr('disabled', true);

Or use ng-disabled directive. this is the best way to disable an element in angular.

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.