Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javascript script getElementsByClassName issue

Posted on 2011-02-22
4
Medium Priority
?
384 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 2000 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 83

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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 …
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…

783 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