Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •