Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 395
  • Last Modified:

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




0
error77
Asked:
error77
1 Solution
 
chaitu chaituCommented:
var myDiv = document.getElementsByClassName('myClass');
for(i=0;myDiv.length;i++)
myDiv[i].style.width = swidth + 'px';

Open in new window


0
 
ProculopsisCommented:
It's an array so...

// untested
var i = -1;
while ( ++i != myDiv.length ) {
  myDiv[ i ].style.width = "1px";
}
0
 
leakim971PluritechnicianCommented:
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

0
 
error77Author Commented:
Perfect thanks :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now