Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-13-2010, 03:59 PM   PM User | #1
altergothen
New to the CF scene

 
Join Date: Dec 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
altergothen is an unknown quantity at this point
Looping through Form Elements?

Hi there

I have a form with elements named (& id) 1 through 5
Why can't I loop through all my form elements like this...

Code:
function toggle(){
	for (var i=1;i<=5;i++)
	{
		var y = document.getElementById(i);
		y.style.backgroundColor = '#FFFFFF';		

	}
}
The error I get is y is null???
If I hard code the id's like this...
var y = document.getElementById('2');
it works fine??

Please Help

NEWB
altergothen is offline   Reply With Quote
Old 12-13-2010, 04:10 PM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
var y = document.getElementById(i);
Validate your markup first. You can't have the value of an id that starts with a number.
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 12-13-2010, 04:14 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,105
Thanks: 197
Thanked 2,422 Times in 2,400 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Form names and ids may not start with a digit.

Try this:-


Code:
<form name = "myform">
<input name="Field1" type="text" /><br>
<input name="Field2" type="text" /><br>
<input name="Field3" type="text" /><br>
<input name="Field4" type="text" /><br>
<input name="Field5" type="text" /><br>
<input type = button value = "Change Background Color" onclick = "chk()">
</form>

<script type = "text/javascript">
function chk() {
for(var i=1; i<=5; i++){
document["myform"]["Field" + i].style.backgroundColor = '#FF0000';		
}
}
</script>
or if you want to use ids:-

Code:
<script type = "text/javascript">
function chk() {
for(var i=1; i<=5; i++){
document.getElementById("Field" + i).style.backgroundColor = '#FF0000';		
}
}
</script>

Quote:
Originally Posted by altergothen View Post
The error I get is y is null???
If I hard code the id's like this...
var y = document.getElementById('2');
it works fine??
Yes it does, because '2' is a string value, not a number. But very srongly advised to avoid.

Nothing's black or white in our country - you're either brilliant or you're hopeless. - Commentator, Radio 5 Live

Last edited by Philip M; 12-13-2010 at 04:22 PM..
Philip M is online now   Reply With Quote
Old 12-13-2010, 04:16 PM   PM User | #4
DrDOS
Senior Coder

 
Join Date: Sep 2010
Posts: 1,234
Thanks: 11
Thanked 157 Times in 157 Posts
DrDOS is infamous around these parts
That's because when you hard code it 2 is a string and not a number. Better to not use numbers as IDs or names, try this:
Code:
function toggle(){
	for (var i=1;i<=5;i++)
	{
		var y = document.getElementById("form_item"+i);
		y.style.backgroundColor = '#FFFFFF';		

	}
}
where the names and IDs are form_item1, etc., of course you can use any text string you want.
DrDOS is offline   Reply With Quote
Old 12-14-2010, 02:59 PM   PM User | #5
altergothen
New to the CF scene

 
Join Date: Dec 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
altergothen is an unknown quantity at this point
Thumbs up Thanks Guys

Thanks Guys!!!
you have been a great help ;-)
altergothen is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:33 PM.


Advertisement
Log in to turn off these ads.