SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    using an array in document.form....

    I have writtem the following function to set the value of a text field according to which check box is checked:

    Code:
    function setAccessLevel(){
    var accessLevel =""
    var x=0
    var a= new Array("district", "region", "area", "deputy", "coord")
    	while (accessLevel=="") {		
    		if (x==a.length){
    			accessLevel="member";		
    		}
    		if (document.form1.a[x].checked==true){
    			accessLevel=a[x];
    		}
    		x++;
    	}
    document.form1.access2.value=accessLevel;
    }
    the line document.form1.a[x].checked==true is not working properly - any ideas?

    monkey
    monkey - the rest is history

  2. #2
    SitePoint Addict
    Join Date
    Nov 2003
    Location
    Malmoe, Sweden
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a is an array not a reference to a checkbox object. Do you have any checkboxes?

  3. #3
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the value of the array is the name of the checkbox!!

    a[0]=district, a[1]=region etc...

    monkey
    monkey - the rest is history

  4. #4
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Try the following:

    Code:
    if (document.form1.elements[a[x]].checked==true){
    Yours, Erik.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  5. #5
    SitePoint Addict
    Join Date
    Nov 2003
    Location
    Malmoe, Sweden
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that should do the trick. monkey, you probably could use the eval()-function as well but that is considered pretty "bad practice". Erik┤s example is more the "right way".

  6. #6
    SitePoint Wizard boxhead's Avatar
    Join Date
    Feb 2004
    Location
    UK
    Posts
    1,040
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just the job!! Thanks muchly.

    monkey
    monkey - the rest is history


Bookmarks

Posting Permissions

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