2

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?

dda
  • 6,030
  • 2
  • 25
  • 34
David Willis
  • 711
  • 3
  • 11
  • 19

4 Answers4

14

It's a string:

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

Bye

P.S. It's an array

Chuck
  • 234,037
  • 30
  • 302
  • 389
Enrico Murru
  • 2,313
  • 4
  • 21
  • 24
3

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

If the 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.

dda
  • 6,030
  • 2
  • 25
  • 34
kennebec
  • 102,654
  • 32
  • 106
  • 127
2

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
  • 377,238
  • 77
  • 533
  • 578
1

Try saying:

document.getElementsByClassName("tab_panel")
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284