473,406 Members | 2,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

getElementsByClassName

I want to change the styling of several <div> elements with just one
onMouseOver event.
"getElementsByClassName" would be ideal if it hadn't been rejected.
I can't seem to make getElementsByTagName work and all the references I've
seen don't help.

The page in question has a variable number of <div> elements because I
generate it with a server-side vbscript.

I could create sequentially numbered IDs and write a short javascript to
loop through the ID numbers (example below) but is there a more elegant
solution?

eg:
<div id="ref1" class="myclass"...
<div id="ref2" class="myclass"...
<div id="ref3" class="myclass"...
<div id="ref4" class="myclass"...
<div id="ref5" class="myclass"...

then
for(i=1;i<=5;i++)
{
idRef = "ref" + i
document.getElementById(idRef).style.blahblahblah = morestuf
}
Jul 23 '05 #1
3 2665
On Fri, 27 Aug 2004 16:56:05 +0100, Danny@Kendal
<da***@STOPSPAMghpkendal.co.uk> wrote:

01234567890123456789012345678901234567890123456789 01234567890123456789
I want to change the styling of several <div> elements with just one
onMouseOver event. "getElementsByClassName" would be ideal if it hadn't
been rejected. I can't seem to make getElementsByTagName work and all
the references I've seen don't help.
Seeing your code might help to see what went wrong. Try:

function getRefByTagName(n, b) {
b = b || document;
if(b.getElementsByTagName) {
return b.getElementsByTagName(n);
} else if(b.all && b.all.tags) {
if('*' == n) {return b.all;}
else {return b.all.tags(n) || [];}
} else {return [];}
}

function myFunction() {
var cN, d = getRefByTagName('DIV');

for(var i = 0, n = d.length; i < n; ++i) {
if('string' == typeof (cN = d[i].className)
&& -1 != cN.indexOf('myclass'))
{
// do stuff with d[i]
}
}
}

(Untested)

[snip]
document.getElementById(idRef).style.blahblahblah = morestuf


Don't forget to feature detect getElementById and the style object before
using them.

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
"Danny@Kendal" wrote:
I want to change the styling of several <div> elements with just one
onMouseOver event.
"getElementsByClassName" would be ideal if it hadn't been rejected.
I can't seem to make getElementsByTagName work and all the references I've
seen don't help.

The page in question has a variable number of <div> elements because I
generate it with a server-side vbscript.

I could create sequentially numbered IDs and write a short javascript to
loop through the ID numbers (example below) but is there a more elegant
solution?

eg:
<div id="ref1" class="myclass"...
<div id="ref2" class="myclass"...
<div id="ref3" class="myclass"...
<div id="ref4" class="myclass"...
<div id="ref5" class="myclass"...

then
for(i=1;i<=5;i++)
{
idRef = "ref" + i
document.getElementById(idRef).style.blahblahblah = morestuf
}


You don't need the variable assignment, but what you've got is the way to do
it. Since the number of DIVs is controlled by the server, you can have it sent
a length in client-side javascript:

var numberOfDivs = <%= numberOfDivs %>;
for (var i = 1; i <= numberOfDivs; i++) { // ...

but a better solution would be:

var i = 1, theDiv;
while ((theDiv = document.getElementById('ref' + i++)) != null) {
theDiv.style.color = 'Red';
}

Providing the DIVs are number sequentially starting at 1, your code will
always work, no matter how many DIVs you output. I'm not sure why you couldn't
get a getElementsByTagName() working, it would be (although slower then the
algorithm shown above):

var divs = document.getElementsByTagName('div');
if (divs) {
for (var i = 0; i < divs.length; i++) {
if (divs[i].className == 'myclass') {
divs[i].style.color = 'Red';
}
}
}

Lastly, you might be better off having two classes, one with each set of
styles, in which case you can do:

var i = 1, theDiv;
while ((theDiv = document.getElementById('ref' + i++)) != null) {
theDiv.className = 'myclassWithColorRed';
}

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #3
"Grant Wagner" <gw*****@agricoreunited.com> wrote in message
news:41***************@agricoreunited.com
"Danny@Kendal" wrote:
I want to change the styling of several <div> elements with just one
onMouseOver event.
"getElementsByClassName" would be ideal if it hadn't been rejected.
I can't seem to make getElementsByTagName work and all the
references I've seen don't help.

The page in question has a variable number of <div> elements because
I generate it with a server-side vbscript.

I could create sequentially numbered IDs and write a short
javascript to loop through the ID numbers (example below) but is
there a more elegant solution?

eg:
<div id="ref1" class="myclass"...
<div id="ref2" class="myclass"...
<div id="ref3" class="myclass"...
<div id="ref4" class="myclass"...
<div id="ref5" class="myclass"...

then
for(i=1;i<=5;i++)
{
idRef = "ref" + i
document.getElementById(idRef).style.blahblahblah = morestuf
}


You don't need the variable assignment, but what you've got is the
way to do it. Since the number of DIVs is controlled by the server,
you can have it sent a length in client-side javascript:

var numberOfDivs = <%= numberOfDivs %>;
for (var i = 1; i <= numberOfDivs; i++) { // ...

but a better solution would be:

var i = 1, theDiv;
while ((theDiv = document.getElementById('ref' + i++)) != null) {
theDiv.style.color = 'Red';
}

Providing the DIVs are number sequentially starting at 1, your code
will always work, no matter how many DIVs you output. I'm not sure
why you couldn't get a getElementsByTagName() working, it would be
(although slower then the algorithm shown above):

var divs = document.getElementsByTagName('div');
if (divs) {
for (var i = 0; i < divs.length; i++) {
if (divs[i].className == 'myclass') {
divs[i].style.color = 'Red';
}
}
}

Lastly, you might be better off having two classes, one with each set
of styles, in which case you can do:

var i = 1, theDiv;
while ((theDiv = document.getElementById('ref' + i++)) != null) {
theDiv.className = 'myclassWithColorRed';
}


That would do it very, very nicely. Many thanks. Now I'm looking forward to
getting back to work after the bank holiday weekend. :-)

I think the getElementsByTagName didn't work because I was trying to use it
like getElementById. Having seen your example above it has explained a lot.

--
FZS600 - Silver/Black
GS125 - Black/Rust
Ford 100E Prefect - Black, naturally
Whisky - Aberlour Cask Strength
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Derek Basch | last post by:
I have a simple XHTML document that I would like to apply javascript functionality to: <body> <div class="length">9</div> <div class="sequence">AAAAAAATTTAAA</div> <div...
2
by: Lachlan Hunt | last post by:
Hi, I'm trying to implement getElementsByClass() for the document object and the Element interface, that supposed to work for HTML, XHTML, SVG and MathML class attributes (including mixed...
2
by: Newton | last post by:
I'm having some issues with a greasemonkey script i've made (nothing serious, and you'll notice that by the mess). I made it to learn javascript properly at first but it ended handy and now i'm...
26
by: johkar | last post by:
I need to cancel the link and execute a function onclick of all the links within the span tag which has a class of "container" assigned. There will be only one span tag with this class applied. ...
1
by: RobG | last post by:
Just a quick note: Firefox 3 will have getElementsByClassName support and it should be in the HTML 5 specification too. <URL: https://bugzilla.mozilla.org/show_bug.cgi?id=357450#c51 > <URL:...
8
by: tbobker | last post by:
Because of the nature of what im trying to do, the ids of the divs im creating are all from a foreach loop array but the div is classed so when i click an onsubmit i need to find all the IDs for a...
2
by: =?iso-8859-1?q?Jean-S=E9bastien?= | last post by:
Hello, I'm using yahoo css template and designing application with ROR. I'd like to access the elements with no explicit id (starred lines), and i must not add some explicit id. ...
9
by: Dahak | last post by:
I'm trying to generate dynamic functions to use as separate callbacks for an AJAX API call. The API doesn't seem to allow for the inclusion of any parameters in the callback, so I can't...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.