Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Michael Paccione
9,017 PointsGet CSS style values with Javascript
How do you get an element's css values with vanilla javascript. I want to write conditionals based upon them.
Logan Wu
10,792 PointsOr you can also try jQuery: http://api.jquery.com/css/
For example, by using the .css( propertyName ).
Michael Paccione
9,017 PointsWrite is not the way and I don't want to load jquery for a one line issue. Thanks for the time.
1 Answer
Roberto Alicata
Courses Plus Student 39,959 Pointsdocument.getElementById("MyElement").className;
if you need more info go to this link
Michael Paccione
9,017 PointsInteresting. Ive used className = '' before to set the xlass but didnt know about .className. to get the class styling.
Using that same relation I'm looking for getting the background styles of an element that doesnt have a class. To set the styles its elem.style.background = ''. What is it to get the style? Is the above relationship the same? Elem.style.background
Ultimately im looking to create conditionals in js based on css properties that arent attached by class.
Logan Wu
10,792 PointsLogan Wu
10,792 PointsWill
document.write(' ')with the element's attributes inside work?