abhit
abhit

Reputation: 981

javascript disabled attribute not working in angularJS?

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

Upvotes: 1

Views: 1737

Answers (5)

Punith Mithra
Punith Mithra

Reputation: 628

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.

Upvotes: 0

Cleiton
Cleiton

Reputation: 18113

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');

Upvotes: 2

Hikmat Sijapati
Hikmat Sijapati

Reputation: 6994

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

Upvotes: 0

clinton3141
clinton3141

Reputation: 4841

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.

Upvotes: 3

strwoc
strwoc

Reputation: 531

Angular has built in jQuery, so you could just add

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

Upvotes: 1

Related Questions