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

Changing variable name using a loop

1
Hi!

I'm new to Javascript and I'm trying to write a script in which I need to use diferent variable names. The names are used from here:

<img border="0" alt="Visit W3Schools!" src="052407-ektron-pmsa-authentication.gif" name="b1" width="26" ...

I have several images which go from b1 to b5 but I want to be able to change that. For example, to add or remove images with almost no code changes.

I use a javascript to change the atributes of the image. For example:

document.b1.width=50;

I would like to be able to use a loop to change the atributes from all images without having to use a code line for each one (for example with a loop, using a variable to change the name b1, b2...).

Thanks in advance.
Dec 18 '07 #1
1 3624
gits
5,390 Expert Mod 4TB
hi ...

instead of using the name-attribute use the id:

[HTML]<img border="0" alt="Visit W3Schools!" src="052407-ektron-pmsa-authentication.gif" id="b1" width="26"/>[/HTML]
now you may use a function like this:

Expand|Select|Wrap|Line Numbers
  1. function set_width() {
  2.     for (var i = 1; i <=5; i++) {
  3.         var img = document.getElementById('b' + i);
  4.         img.width = 'whatever_value';
  5.     }
  6. }
  7.  
kind regards
Dec 19 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
1
by: Ken Fine | last post by:
I have a menu system that has nodes that can be opened or closed. In an effort to make my code more manageable, I programmed a little widget tonight that keeps track of the open/active item and...
8
by: horos | last post by:
hey all, Ok, a related question to my previous one on data dumpers for postscript. In the process of putting a form together, I'm using a lot of placeholder variables that I really don't care...
16
by: chris | last post by:
im new to javascript but slowly getting better what i want to do is have some text on the screen and when an event happens for example click a button the text would change to what i want. how...
10
by: Altman | last post by:
I have only done a little programming in C++ so I am still learning but I am having a problem with a variable that is changing on me. I have tried this 2 ways with the same result. I have a...
1
by: John_H | last post by:
Re: ASP.NET 2.0 I would like suggestions or code examples on how to collect a variable length list of input data (item# & item quantity specifically). I thought that I could accomplish this...
6
by: Jody Gelowitz | last post by:
I have run into an issue with variable scope within an XSLT document that is translated in VS.NET 2.0. Under VS.NET 1.1 (XslTransform), this code works fine. However, when using VS.NET 2.0...
2
by: mcpete | last post by:
Hi all, I've been searching for a while, and have not been able to find a working solution. I'm trying to change the name of multiple strings within some serverside vbscript, depending on the...
3
TheServant
by: TheServant | last post by:
Hey guys, Basically I have a really big table with lots of variables all over the place. eg. <table> <tr><td><?php echo( $var1." is ".$var2 ); ?></td></tr> <tr><td><?php echo( $bar1." is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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,...

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.