473,326 Members | 2,175 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,326 software developers and data experts.

Cycle through all DIVs, please help

Hi to all,

I have several DIVs in a page, each has a unique name. I'd need a method to
cycle through all DIVs so that I can change their style. For example, let's
say I need to set a red background to DIVs whose name begins with "a". This
is the pseudo code:

For Each Div in The Page
If Div's name starts with 'a' then set Div's background to Red
Next

How can I translate this 'pseudocode' into real javascript code?
Thank you!

Marc
Jul 23 '05 #1
3 13759


Marc wrote:
I have several DIVs in a page, each has a unique name. I'd need a method to
cycle through all DIVs so that I can change their style. For example, let's
say I need to set a red background to DIVs whose name begins with "a". This
is the pseudo code:

For Each Div in The Page
If Div's name starts with 'a' then set Div's background to Red
Next

How can I translate this 'pseudocode' into real javascript code?


<div> elements in HTML don't have a name attribute so you should use the
id attribute e.g.
<div id="a1">...</div>
You can find out all the <div> elements in the document with
var divs = document.getElementsByTagName('div');
then loop
for (var i = 0; i < divs.length; i++) {
var div = divs[i];
if (div.id.indexOf('a') == 0 && div.style) {
div.style.backgroundColor = 'red';
}
}

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #2
Ivo
"Marc" wrote
I have several DIVs in a page, each has a unique name. I'd need a method to cycle through all DIVs so that I can change their style. For example, let's say I need to set a red background to DIVs whose name begins with "a". This is the pseudo code:

For Each Div in The Page
If Div's name starts with 'a' then set Div's background to Red
Next

How can I translate this 'pseudocode' into real javascript code?
Thank you!

Marc


var els = document.getElementsByTagName('div');
var i = els.length; while( i-- ) {
if( els[i].indexOf('a') === 0 ) {
els[i].style.background = 'red';
}
}

You could use an incrementing "for" loop, but a decrementing "while" is
faster.
HTH
Ivo
Jul 23 '05 #3
Ivo
"Ivo"wrote
if( els[i].indexOf('a') === 0 ) {


Too fast! That should of course have been:
if( els[i].name.indexOf('a') === 0 ) {
if you are happy with named div's. I 'd use id's.
Ivo
Jul 23 '05 #4

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

Similar topics

2
by: m3ckon | last post by:
Hi there, had to rush some sql and am now going back to it due to a slow db performance. I have a db for sales leads and have created 3 views based on the data I need to produce. However one...
6
by: James Walker | last post by:
Can some one help I get an error of 'checkIndate' is null or not an object can someone please help. I can't work out why Thanks in advance James <form> <td height="24" colspan="7"...
0
by: Kurt Watson | last post by:
I’m having a different kind of problem with Hotmail when I sign in it says, "Web Browser Software Limitations Your Current Software Will Limit Your Ability to Use Hotmail You are using a web...
7
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help...
1
by: Steve | last post by:
Hi, I've asked this question a couple of times before on this forum but no one seems to be nice enough to point me to the right direction or help me out with any information, if possible. Please...
22
by: KitKat | last post by:
I need to get this to go to each folders: Cam 1, Cam 2, Cam 4, Cam 6, Cam 7, and Cam 8. Well it does that but it also needs to change the file name to the same folder where the file is being...
5
nabh4u
by: nabh4u | last post by:
hi, i have a program where every thing is working properly. i have a vector with some values. i use iterators and delete a specific value in the vector. here the loop runs infinitely only for some...
6
by: jenipriya | last post by:
Hi all... its very urgent.. please........i m a beginner in oracle.... Anyone please help me wit dese codes i hv tried... and correct the errors... The table structures i hav Employee (EmpID,...
1
by: stevejhon | last post by:
Hi, I have content page. A master page is applied to it. On the content page, i have button and i want to handle its click event. My function is ready on click event and 'Click'attribute is also...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.