Link to home
Start Free TrialLog in
Avatar of error77
error77

asked on

Javascript script getElementsByClassName issue

Hi all,

I am trying the following:

var myDiv = document.getElementsByClassName('myClass');
alert(myDiv);

This is returning: [object HTMLCollection]

I need to change all the width's of a class on my page...

myDiv.style.width = swidth + 'px';

It works on getElementById but not in Classes ... how can I get this to work please?

THanks




ASKER CERTIFIED SOLUTION
Avatar of chaitu chaitu
chaitu chaitu
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Proculopsis
Proculopsis

It's an array so...

// untested
var i = -1;
while ( ++i != myDiv.length ) {
  myDiv[ i ].style.width = "1px";
}
Avatar of leakim971
If you need to use getElementsByClassName on IE : http://robertnyman.com/2008/05/27/the-ultimate-getelementsbyclassname-anno-2008/
Else with jQuery :

$(".myClass").css({"width":swidth + 'px'});

Open in new window

Avatar of error77

ASKER

Perfect thanks :)