Solved

How do you save information from a Javascript generated form, which depends on a regular form and the onChange() function?

Posted on 2009-05-15
1
420 Views
Last Modified: 2012-05-07
I'm hoping that someone can help me with a HTML/Javascript problem I'm having.

The code below displays a drop down menu with the options ('1', '2', '3').

Selecting any one of the options results in the use of the onChange function of the HTML form to access a Javascript that generates an additional text box form. The number of text boxes that are generated depend on the number selection in the drop down menu. '1' text box is generated by default.

THE PROBLEM: Let's say I write something in the 'First Name:' field. Then let's say I alter my selection in the drop down menu form. This results in the information I entered disappearing. I want to maintain this information and reload it into the regenerated form but can't find a way to do it. I would really appreciate any help.

ADDITIONAL INFO: I understand that it would be very simple in this case for the user to just re-enter the data, but the current code is a simple example of a more complicated form that I'm working on.

Thanks.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 
<script type="text/javascript">
 
function tryIt(param) {
	var content = "";	
	
	if(param>0) {
		if(document.getElementById){
			for(i=0; i<param; i++) {
			//Generates part of the form that looks like -> First Name:<input type="text" name="fname1" id="a1">
				content = content + "<br />First Name:<input type=\"text\" name=\"fname"+i+"\" id=\"a"+i+"\" >";
			}
		}
		document.getElementById('outputs').innerHTML = content;
	}	
}
 
</script>
 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
 
<form action="randomLink.asp" method="get">
	<select name="modifyData" onChange="tryIt(this.options[this.selectedIndex].value)">
    	<option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
    <br />
    <div id="outputs">
	</div>
</form>
 
 
<!-- The script is called once during the page load so that the form is generated with 1 as the default input -->
 
<script type="text/javascript">
tryIt(1);
</script>
 
 
</body>
</html>

Open in new window

0
Comment
Question by:smik011
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 14

Accepted Solution

by:
sam2912 earned 250 total points
ID: 24395943
change your function:
<script type="text/javascript">
 
function tryIt(param) {
	var values = new Array();
		// save values
                if(document.getElementById){
                        for(i=0; i<param; i++) { try {
                        	e=eval(document.getElementById("a"+i));
				values[i]=e.value;
                        } catch(e) {}}
                }
 
        var content = "";       
        
        if(param>0) {
                if(document.getElementById){
                        for(i=0; i<param; i++) {
				// Get saved value if available
				var val="";
				(values[i]&&values[i]!="undefined")?val=values[i]:val="";
				
                        	//Generates part of the form that looks like -> First Name:<input type="text" name="fname1" id="a1">
                                content = content + "<br />First Name:<input type=\"text\" name=\"fname"+i+"\" id=\"a"+i+"\" value=\""+val+"\" >";
                        }
                }
                document.getElementById('outputs').innerHTML = content;
        }       
}
 
</script>

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question