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

abhit
  • 973
  • 3
  • 17
  • 40

5 Answers5

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.

clinton3141
  • 4,751
  • 3
  • 33
  • 46
  • the element is dynamically created how can i use ng-disabled on it? – abhit Jan 20 '17 at 12:50
  • You can use [$compile](https://docs.angularjs.org/api/ng/service/$compile) to create dynamic elements which are still angular-able. Related question: http://stackoverflow.com/questions/15279244/dynamically-add-directive-in-angularjs – clinton3141 Jan 20 '17 at 12:57
  • 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... – abhit Jan 20 '17 at 13:03
  • 1
    update your question if a jsfiddlerso we may help you. – Cleiton Jan 20 '17 at 13:04
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');
Cleiton
  • 17,663
  • 13
  • 46
  • 59
1

Angular has built in jQuery, so you could just add

$(".pit").attr('disabled', true);
strwoc
  • 511
  • 1
  • 9
  • 18
0
angular.element(document.querySelector(".pit")).setAttribute('disabled',true);
Hikmat Sijapati
  • 6,869
  • 1
  • 9
  • 19
  • 1
    Although this code might solve the problem, one should always consider adding an explanation to it. – BDL Jan 20 '17 at 13:57
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.

Punith Mithra
  • 608
  • 5
  • 9