473,587 Members | 2,267 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13772


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.getEle mentsByTagName( '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.backg roundColor = '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.getEle mentsByTagName( 'div');
var i = els.length; while( i-- ) {
if( els[i].indexOf('a') === 0 ) {
els[i].style.backgrou nd = '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
1921
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 o the views, which has subqueries to the other views is VERY slow and it needs to be speeded up, but am unsure how, can anyone help... below is the...
6
1781
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" valign="top"><form name="booknow"><select
0
1691
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 browser that Hotmail does not support. If you continue to use your current browser software we cannot guarantee that Hotmail will work correctly for...
7
2378
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 me. This was inspired by Exercise 7 and Programming Problem 8 in Chapter 3 of our text. I have done Exercise 7 for you: Below you will find the...
1
1375
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 help me out with this because this is a very serious issue for me and I need to learn more about this. And here it is again: I've been running...
22
2174
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 grabbed, BUT it doesn't. I have tried and tried.....please help example: C:\Projects\Darryl\Queue Review Files\2-24\Cam 7\Cam7-20060224170000-01.jpg ...
5
1679
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 values. i dont know what is the problem. sample code: vector<int>::iterator Iterator; for( Iterator = vect.cmatch.begin(); ...
6
3303
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, EmpName,DeptID,DateOfJoin, Sal, Addr) Finance (EmpID, Sal) Club (Clubname, EmpID, Fee, DateOfJoin) Leave (EmpID, Date) Department (DeptID, DeptName,...
1
395
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 set for the button. As per my assumption everything should work fine, but when i run the page, and click the button, nothing happens, the button...
0
7915
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8339
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.