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 3 of 3
  1. #1
    New Coder
    Join Date
    Apr 2003
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Populate form fields using a for loop

    I managed to get this far with it but I'm struggling with the last two lines. Here's the bit of code I'm working on:


    Code:
    	if (data != "") {
    		var fields=data.split("\r" )
    		for(var i=1; i<6; i++) {
    			var desturl = fields[i].substring(16,fields[i].indexOf("@"));
    			var lnktext = fields[i].substring(fields[i].indexOf("@")+1,fields[i].length-1);
    			document.myform.link1url.value=desturl;
    			document.myform.link1desc.value=lnktext;
    		}
    	}

    As it is, link1url & link1desc get the info each time through the loop but the form fields are:

    link1url & link1desc
    link2url & link2desc
    link3url & link3desc
    link4url & link4desc
    link5url & link5desc

    So, can I replace the number in the middle of the field name with i each time it loops?

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I suspect you want something like this
    Code:
    var elems = document.myform.elements;
    if (data != "") {
    	var fields=data.split("\r" )
    	for(var i=1; i<6; i++) {
    		var desturl = fields[i].substring(16,fields[i].indexOf("@"));
    		var lnktext = fields[i].substring(fields[i].indexOf("@")+1,fields[i].length-1);
    		elems['link' + i + 'url'].value=desturl;
    		elems['link' + i + 'desc'].value=lnktext;
    	}
    }
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #3
    New Coder
    Join Date
    Apr 2003
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cheers, beetle - that works great.


  •  

    Posting Permissions

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