24

For some reasons, I can't change the style of an element using the following :

angular.element("#element").style.height = 100px;

I'm sure that angular.element("#element") works, as it returns a DOM element. Plus, this works :

angular.element("#element").addClass('something');

(Everything I've found is about ngStyle but I don't think it is what I'm looking for ?)

Should I use something else ?

If so : What ? Why .style.something doesn't work ?

1
  • You sure it is working as you describe. You must be loading jQuery yes? Commented Jun 23, 2014 at 12:04

3 Answers 3

57

According to the docs:

Note: all element references in Angular are always wrapped with jQuery or jqLite; they are never raw DOM references.

So the .style property is not available directly, since it is a property of the wrapped HTMLElement.


You can either use ivarni's approach to get hold of the HTMLElement (angular.element(...)[0].style...) or use jQuery's/jqLite's .css() method:

angular.element('#element').css('height', '100px');
Sign up to request clarification or add additional context in comments.

5 Comments

Well im trying to use angular.element(el).css("-webkit-transform","translate3d(200px,0,0)") and its not working at all.. seems that jqlite is not parsing "-".
I just discovered that the !important was the problem. Don't know why
Doesn't work for me. This works: angular.element(document.querySelector("#element")).css("height", "100px").
@stéphane-laurent: You are probably not using jQuery. With jqLite you can only find elements by tag name (as mentioned in the docs linked to in the answer)
This works for changing height or width but not working with changing background-color of div with given id(#).Kindly suggest
12

Angular's element returns something that wraps a DOM element, not the DOM element itself. You can access the underlying element much like you would with a JQuery selector.

angular.element("#element")[0].style.height = 100px;

That of course assumes you've got one match, which you should when using an ID.

5 Comments

What? Don't you get "Looking up elements via selectors is not supported by jqLite!..."
Not if you load JQuery before angular, which he obviously did, or he would be asking a different question.
See docs.angularjs.org/api/ng/function/angular.element, "To use jQuery, simply load it before DOMContentLoaded event fired".
No need to get snooty @ivarni. If OP was knowingly loading jQuery his question would have asked about jQuery, not Angular. Even you don't seem to realize that the return object is not "much like" jQuery. It is jQuery.
I wasn't trying to be "snooty", I was just stating that if he hadn't loaded JQuery then he would probably be asking why the selector didn't work. Since he explicitly said the selector worked that means he did load it.
1

If you are running Angular without jQuery, this works:

angular.element(document.querySelector("#element"))[0].style.height = "100px";

I have tested this in an ionic application.

1 Comment

This works, but you are getting the element twice, once with .element and once with .querySelector

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.