What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » using js to add a form input, but retaining typed info?

using js to add a form input, but retaining typed info?
Author: Message:
stoshrocket
Senior Member
****

Avatar
formerly methos

Posts: 748
Reputation: 31
29 / Male / Flag
Joined: Aug 2005
O.P. using js to add a form input, but retaining typed info?
I've quickly knocked up the following:
HTML code:
<script type="text/javascript">
function add_input(){
document.getElementById('input_container').innerHTML += '<input type="text"><br />';
}
</script>
 
<div>
<form>
<div id="input_container">
<input type="text"><br />
</div>
<br /><input type="submit" value="submit">
</form>
<a href="#" onclick="add_input()">add input</a>
</div>

...which just adds a form input when you click the add input link. However, I want to be able to retain the information pre-entered in the inputs, so if I typed something into the first input, click add another, and I would have whatever I typed in the first plus another input (currently it just replaces the div contents with a new set of inputs). Any ideas?

This post was edited on 07-19-2009 at 10:46 PM by stoshrocket.
formerly methos
07-19-2009 10:27 PM
Profile PM Web Find Quote Report
felipEx
Scripting Contest Winner
***


Posts: 378
Reputation: 24
31 / Male / Flag
Joined: Jun 2006
RE: using js to add a form input, but retaining typed info?
Use createElement and appendChild

Javascript code:
function extend(m, e){
    var e = e || this;
    for (var x in m) e[x] = m[x];
    return e;
};
 
function create(type, opt, parent){
    var el = document.createElement(type);
 
    if (opt.style){
        extend(opt.style,el.style);
    }
 
    delete opt.style;
 
    extend(opt,el);
 
    if (parent){
        parent.appendChild(el);
    }
 
    return el;
}
 
 
function add_input(){
create("input", {type: "text"}, document.getElementById('input_container'));
create("br", {},  document.getElementById('input_container'));
//document.getElementById('input_container').innerHTML += '<input type="text"><br />';
}


this should do the trick :)

This post was edited on 07-20-2009 at 02:30 PM by felipEx.
07-19-2009 11:14 PM
Profile E-Mail PM Find Quote Report
stoshrocket
Senior Member
****

Avatar
formerly methos

Posts: 748
Reputation: 31
29 / Male / Flag
Joined: Aug 2005
O.P. RE: using js to add a form input, but retaining typed info?
Perfect (Y) thanks very much
formerly methos
07-20-2009 03:13 PM
Profile PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On