tags:

views:

43

answers:

4

Hello I am trying to access the width of a div to put in a cookie. This is the div:

 <div class="tab_panel" style="width:600px">

It is the only div with this class name. It is not an option to specify a unique id for the div. This is the code I have been using in an event to call it but it gives an error

 document.getElementsByClassName(tab_panel).style.width

I know firefox supports getElementsByClassName so what am I doing wrong?

A: 

Try saying:

document.getElementsByClassName("tab_panel")
Justin Ethier
A: 

Missing quotes:

document.getElementsByClassName('tab_panel').....

You should iterate over all elements like this:

var elms = document.getElementsByClassName('tab_panel');

for(var i = 0 ; i < elms.length; i++)
{
   alert(elms[i].style.width);
}
Sarfraz
+4  A: 

It's a string:

document.getElementsByClassName("tab_panel")[0].style.width

Bye

P.S. It's an array

Enreeco
+1, nice catch on the array
Justin Ethier
+1  A: 

document.getElementsByClassName("tab_panel") returns a collection of nodes, the first of which is referred to by document.getElementsByClassName("tab_panel")[0].

If tha node you are searching does not have an inline style="width:' assignment, an empty string is returned from document.getElementsByClassName("tab_panel")[0].style.width.

kennebec