Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

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 01-14-2009, 10:23 AM   PM User | #1
durrrr
New to the CF scene

 
Join Date: Jan 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
durrrr is an unknown quantity at this point
Incrementing an array

Hi

I am trying to increment an array with a new id value everytime the criteria within my for loop and if statement is met. is my syntax for the array correct? i think the value in the array is being overwritten everytime the loop goes around? any help is most appreciated!

thanks alot


code:

var idStr;

var idArr = new Array ("1","2","3","4","5");

var checkedArray = new Array();

for (i = 0; i < idArr.length; i++) {
idStr = idArr[i];

if (document.getElementById(idStr) != null && document.getElementById(idStr).checked == true){


checkedArray = idStr;


}
}

thanks alot
durrrr is offline   Reply With Quote
Old 01-14-2009, 03:16 PM   PM User | #2
itsallkizza
Senior Coder

 
Join Date: Oct 2008
Location: Long Beach
Posts: 1,196
Thanks: 36
Thanked 164 Times in 164 Posts
itsallkizza will become famous soon enough
This will do what you want:
Code:
var id_arr = ["1","2","3","4","5"];
var checked_arr = new Array();
for (var i=0;i<id_arr.length;i++)
	{
	var element_to_check = document.getElementById(id_arr[i]);
	if (element_to_check && element_to_check.checked) checked_arr.push(id_arr[i]);
	}
You shouldn't be giving your html elements ids that start with a number though.

Array.push(value) adds value to the end of the array instance.
__________________
Feel free to e-mail me if I forget to respond ;)
ohsosexybrit@gmail.com
itsallkizza 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:12 PM.


Advertisement
Log in to turn off these ads.