/    Sign up×
Community /Pin to ProfileBookmark

Accessing a css style attribute…

Hello,

I have a table cell with an associated css style, and I need to read the style attributes from a javascript routine. Could someone provide some pointers on how this is done?

Thanks!

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@VladdyMay 10.2004 — A link to the page and explanation of what you are trying to achieve by "reading the style attributes from a javascript routine" would be helpful in determining what you actually need.
Copy linkTweet thisAlerts:
@JShineauthorMay 10.2004 — >> A link to the page and explanation of what you are trying to achieve by "reading the style attributes from a javascript routine" would be helpful in determining what you actually need. <<

OK. Let's say we have the following style...

.MainMenuNormal {

font-family: Arial, Helvetica, sans-serif;

font-size: 12px;

color: #FFFFFF;

background-color: #000000;

text-align: center;

vertical-align: middle;

width: 120px;

height: 18px;

}

...and the following table snippet...

<table >

<tr id="MainMenuRow">

<td class="MainMenuNormal" id="mainHome">Home</td>

<td class="MainMenuNormal" id="mainAbout">About Us </td>

<td class="MainMenuNormal" id="mainDepts">Departments</td>

...and so on...

I need to be able to read the cell's style attributes from a javascript - e.g. I need to read the color, background-color, width, height attributes. I may want to change the color, or I may need to position a drop-down menu relative to the position of the table cell.

How does one do this?
Copy linkTweet thisAlerts:
@VladdyMay 10.2004 — To begin with, navigation is a (nested) list of links:

www.vladdy.net/Demos/CSSNav.html

It has nothing to do with a table.

Still you did not explain WHY would you need to read those attributes and WHY would you want to change color.
Copy linkTweet thisAlerts:
@cootheadMay 10.2004 — Hi there JShine,

and welcome to these forums ?

I may be wrong - nothing unusual in that - but I believe that

you can only get the style attributes if they are inline like this...

&lt;script type="text/javascript"&gt;
//&lt;![CDATA[

function displayInfo(){
if( navigator.appName=="Netscape") {
a=document.getElementById('cell').style.getPropertyValue('width');
b=document.getElementById('cell').style.getPropertyValue('height');
c=document.getElementById('cell').style.getPropertyValue('background');
d=document.getElementById('cell').style.getPropertyValue('border');

document.getElementById('cell').innerHTML='width: '+a+'&lt;br /&gt;'+'height : '
+b+'&lt;br /&gt;'+'background : '+c+'&lt;br /&gt;'+'border : '+d;
}
else {
a=document.getElementById('cell').currentStyle.width;
b=document.getElementById('cell').currentStyle.height;
c=document.getElementById('cell').currentStyle.backgroundColor;
d=document.getElementById('cell').currentStyle.borderStyle;
e=document.getElementById('cell').currentStyle.borderWidth;
f=document.getElementById('cell').currentStyle.borderColor;

document.getElementById('cell').innerHTML='width: '+a+'&lt;br /&gt;'+'height : '
+b+'&lt;br /&gt;'+'background : '+c+'&lt;br /&gt;'+'border-style : '+d+'&lt;br /&gt;'
+'border-width : '+e+'&lt;br /&gt;'+'border-color : '+f;
}
}
window.onload=displayInfo;
//]]&gt;
&lt;/script&gt;

&lt;table&gt;&lt;tr&gt;
&lt;td id="cell"style="width:200px; height:100px; background-color:#ff0000; border:solid 1px #000000;"&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;/table&gt;


[i]coothead[/i]
×

Success!

Help @JShine spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...