954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

obtaining style by javascript

okay, I've got another problem.
How can I obtain content of cascade style, using javascript?
I know, there's a code

document.getElementById('id').style.border='5px solid blue';

for example, but I need to do reversed operation, not to change style, but obtain it.
I hope I've described my problem clearly enough enough ;)
Thanks for any help ;)

Willy Blore
Newbie Poster
4 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

Hi Willy

The style object can be used to not only set but get information too. So to reverse your statement, try:

var myBorder = document.getElementById('id').style.border;

The string returned by style.border should look much like your example "5px solid blue", with the border's width, style, and color in a single line.

It's often more convenient to access each attribute individually. For border styles you can do so by going through the style.border property like this:

var myColor = document.getElementById('id').style.border.color;
var myStyle = document.getElementById('id').style.border.style;
var myWidth = document.getElementById('id').style.border.width;


The style object also provides properties that map more directly to attributes, such as style.borderWidth or style.borderLeftWidth. There's a certain amount of duplication here, but you'll find the access offered is sometimes more refined.

Further info: HTML DOM Style Object...

LaxLoafer
Junior Poster in Training
68 posts since Aug 2011
Reputation Points: 43
Solved Threads: 12
 

i will suggestion you to switch to JQUERY because it better,fast and easy to do what ever you want if want it in JQUERY i can tell you how.

extemer
Junior Poster
Banned
188 posts since Apr 2010
Reputation Points: -7
Solved Threads: 10
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You