SoFunction
Updated on 2025-02-28

javascript gets specific CSS attribute value

<link  type="text/css" href="" rel="stylesheet"/> 
<div >
...
</div>

Declared in


#myArticle{
width:400px; 
height:300px;
}

In this case, you can't get the value of 400px directly through JS. because this value is defined in CSS, so you must use other methods. I wrote the following function:


/** 
* function for get the style value in special css file 
* @param int css_file_id 
* @param String labname 
* @param String param 
*/ 
function getStyleValue(css_file_id,labname,param) 

var tar; 
var rss; 
var style; 
var value; 

tar = [css_file_id]; 

rss = ?: 

for(i=0;i<;i ) 

style = rss[i]; 
if(() == ()) 

value = [param]; 


return value; 
}

Now just pass


getStyleValue(0,'#myArticle','width')

You can get it :)