Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old December 5th, 2003, 05:05 PM
Registered User
Join Date: Dec 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Populating Fields on a Form with Cookie Name-Value

I have a multi-page form.

I have in my cookie all the name-value pairs for every field of every form page & I know how to access those values.

When my user goes back to a page already filled in, I would like to populate the actual form *fields* on that page with the values the user filled in on his/her last visit to the page in that session.

In my "onload()" function, I tried:
(where strNames[i] is the name of that field on the page & strValues[i] is the value of that field on the page)

   for (i = 0; i < strNames.length; i++){
      strNames[i].value = strValues[i]; // set value on page
      alert("strNames[i].value = " + strNames[i].value)
   } //

but strNames[i].value was undefined.

How do I populate the actual form fields (as opposed to popping up alert boxes or writing to the bottom of the page with document.write)?
Old December 5th, 2003, 05:15 PM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

You need to figure out the field object based on the name...

     document.forms[0][strNames[i]].value = strValues[i]; // set value on page

I can't say this will definately work (not sure about the element array, it might be form[0].elements[...]), but this is the general idea. You need to use the value from your names array to find the element in the form.

Alternatively you could use the eval() method to find it:

     objFormElement = eval("document.forms[0]."+strNames[i]);
     objFormElement.value = strValues[i]; // set value on page

Lastly, there's the getElementById method, but this work only if you have a value in the ID attribute of each input (I think):

     objFormElement = document.getElementById(strNames[i]);
     objFormElement.value = strValues[i]; // set value on page

(Sorry for the uncertainty of these suggestions. I gave you three in the hopes one would work.)

Work smarter, not harder.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Populating an unbound form from a table Luis Access VBA 8 November 21st, 2008 08:37 AM
Populating Fields from a textbox teddyk ASP.NET 2.0 Basics 1 August 8th, 2008 06:25 AM
Auto Populating Fields in Form E3-PSU Access VBA 3 August 29th, 2007 04:43 AM
populating online text fields etc hamilton C# 2 June 20th, 2007 09:16 AM
Populating a form maneor Access 1 September 30th, 2004 03:41 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.