Solved

Javascript script getElementsByClassName issue

Posted on 2011-02-22
4
370 Views
Last Modified: 2012-05-11
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
Comment
Question by:error77
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 20

Accepted Solution

by:
chaitu chaitu earned 500 total points
ID: 34950947
var myDiv = document.getElementsByClassName('myClass');
for(i=0;myDiv.length;i++)
myDiv[i].style.width = swidth + 'px';

Open in new window


0
 
LVL 20

Expert Comment

by:Proculopsis
ID: 34950963
It's an array so...

// untested
var i = -1;
while ( ++i != myDiv.length ) {
  myDiv[ i ].style.width = "1px";
}
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34950971
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
 

Author Closing Comment

by:error77
ID: 34951011
Perfect thanks :)
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question