Solved

Javascript script getElementsByClassName issue

Posted on 2011-02-22
4
362 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
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to convert from xls to xlsx using java 7 42
JavaScript let vs var 5 27
jQuery Scroll To Top 5 13
how can i get 4 nd 5 ? 3 15
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

832 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