472,807 Members | 3,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,807 software developers and data experts.

add to <input> to <form> with javascript?

Ben
Here's my form:
<form name="aForm" method='post'>
<input type=file name=file1 onkeypress='KeyPress()'><br>
<a
id='attachMoreLink'
href='javascript:AddFileInput()">Attach More Files
</a>
<input type=submit value='Done'>
</form>
Question:
How can I add an <input> field to a <form> when I click on "Attach More Files"?

so, after first button click, the form becomes...
<form name="aForm" method='post'>
<input type=file name=file1 onkeypress='KeyPress()'><br>
<input type=file name=file2 onkeypress='KeyPress()'><br>
<a
id='attachMoreLink'
href='javascript:AddFileInput()">Attach More Files
</a>
<input type=submit value='Done'>
</form>

and so on.... until I click "DONE"

thanks....

Ben
Jul 23 '05 #1
3 12829
One way (but maybe not the best way) to do this is with the innerHTML
property:
http://msdn.microsoft.com/library/de.../innerhtml.asp

Jul 23 '05 #2
Ben wrote:
Here's my form:
<form name="aForm" method='post'>
<input type=file name=file1 onkeypress='KeyPress()'><br>
<a
id='attachMoreLink'
href='javascript:AddFileInput()">Attach More Files
</a>
<input type=submit value='Done'>
</form>
Question:
How can I add an <input> field to a <form> when I click on "Attach More Files"?
so, after first button click, the form becomes...
<form name="aForm" method='post'>
<input type=file name=file1 onkeypress='KeyPress()'><br>
<input type=file name=file2 onkeypress='KeyPress()'><br>
<a
id='attachMoreLink'
href='javascript:AddFileInput()">Attach More Files
</a>
<input type=submit value='Done'>
</form>

and so on.... until I click "DONE"

thanks....

Ben


There have been some support issues with Node.cloneNode() in the past
but they may have been resolved...if so:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css">

div.container {
width: 222px;
font: normal 12px arial;
text-align: center;
padding: 4px;
margin: 2px;
border: 1px #000 solid;
background: #ccc;
}
div.container input {
margin-left: 2px;
}
input.button {
font: normal 11px arial;
margin: 2px 0;
}

</style>
<script type="text/javascript">

var filenum = 1;

function addfile(els)
{
var container,
lastipt = els['file' + filenum],
newipt = lastipt.cloneNode(true);
newipt.name = 'file' + ++filenum;
if (container = document.getElementById('upfiles'))
{
var span = document.createElement('span');
span.appendChild(document.createTextNode(filenum)) ;
container.appendChild(document.createElement('br') );
container.appendChild(span);
container.appendChild(newipt);
els[newipt.name] = newipt; //IE, yech...
}
}

function delfile()
{
if (filenum > 1)
{
var container, i = 3;
if (container = document.getElementById('upfiles'))
{
while (i--)
{
container.removeChild(container.lastChild);
}
--filenum;
}
}
}

function ipt_onkeypress()
{
window.status = 'foo';
}

</script>
</head>
<body>
<form name="aForm" method="post" action="">
<div id="upfiles"
class="container">1<input
class="button"
type="file"
name="file1"
onkeypress="ipt_onkeypress()"></div>
<div class="container"
style="background:#aaa;">
<input
class="button"
type="button"
value="Attach More Files"
onclick="return addfile(this.form.elements)">
<input
class="button"
type="button"
value="Delete Last"
onclick="return delfile()">
</div>
<div class="container"
style="background:#888;">
<input
class="button"
type="submit"
value="Done">
</div>
</form>
</body>
</html>

ie5/mac may require adding that event handler 'manually'.

Jul 23 '05 #3
RobB wrote:
Ben wrote:
Here's my form:
<form name="aForm" method='post'>
<input type=file name=file1 onkeypress='KeyPress()'><br>
<a
id='attachMoreLink'
href='javascript:AddFileInput()">Attach More Files
</a>
<input type=submit value='Done'>
</form>
Question:
How can I add an <input> field to a <form> when I click on "Attach

More Files"?

so, after first button click, the form becomes...
<form name="aForm" method='post'>
<input type=file name=file1 onkeypress='KeyPress()'><br>
<input type=file name=file2 onkeypress='KeyPress()'><br>
<a
id='attachMoreLink'
href='javascript:AddFileInput()">Attach More Files
</a>
<input type=submit value='Done'>
</form>

and so on.... until I click "DONE"

thanks....

Ben


(snip)

On 2nd thought...#:=(

Think a list might be more appropriate.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<style type="text/css">

body {
margin: 40px;
background: #eee;
}
form {
width: 250px;
}
div.container {
width: 98%;
font: normal 12px arial;
text-align: center;
padding: 1px;
margin: 2px 0;
border: 1px #000 solid;
}
div.container ol {
margin: 0;
}
div.container input {
font: normal 11px arial;
margin: 3px 0;
}

</style>
<!--[if IE]>
<style type="text/css">
div.container li {
margin-left: 22px;
}
</style>
<![endif]-->
<script type="text/javascript">

var filenum = 1;

function addfile(els)
{
var container, template;
if ((container = document.getElementById('uplist'))
&& (template = container.getElementsByTagName('li')[0]))
{
var newitem = template.cloneNode(true);
var newipt = newitem.getElementsByTagName('input')[0];
newipt.name = 'file' + ++filenum;
container.appendChild(newitem);
els[newipt.name] = newipt;
}
}

function remfile()
{
if (filenum > 1)
{
var container;
if (container = document.getElementById('uplist'))
{
container.removeChild(container.lastChild);
--filenum;
}
}
}

function ipt_onkeypress()
{
window.status = 'foo';
}

function init()
{
if (document.getElementById
&& document.createElement)
{
var badd, bdel;
if ((badd = document.getElementById('addf'))
&& (bdel = document.getElementById('delf')))
{
badd.disabled = false;
badd.onclick = function()
{
return addfile(this.form.elements);
}
bdel.disabled = false;
bdel.onclick = function()
{
return remfile();
}
}
}
}

window.onload = init;

</script>
</head>
<body>
<form name="aForm" method="post" action="">
<div class="container" style="background:#ccc;">
<ol id="uplist">
<li>
<input
type="file"
name="file1"
onkeypress="ipt_onkeypress()">
</li>
</ol>
<div class="container" style="background:#aaa;">
<input
id="addf"
type="button"
style="width:110px;"
value="Attach More Files"
disabled="disabled">
<input
id="delf"
type="button"
style="width:110px;"
value="Remove Last File"
disabled="disabled">
</div>
<div class="container" style="background:#888;">
<input
type="submit"
value="Done">
</div>
</div>
</form>
</body>
</html>

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Martin Lucas-Smith | last post by:
I am wanting to know whether are XHTML1-valid characters for use within an id attribute and/or a name attribute. ...
5
by: Jonathan Daggar | last post by:
Hello, I'm trying to put together a form with a very tight table formatting. However, every time I put an text-type input field in, the browser pads the area to the right of it with space. I've...
2
by: Keiron Waites | last post by:
I have the following code: <input type="text" name="search" class="search_top"> <a href="" onclick="window.location='search.inc.php'+document..search. value; return false;"...
4
by: owen | last post by:
I have an <input> box and i want to disable the apostrophe ( ' ) key, so when you press it, no character appears in the input box. All other keys should work ok. I can trap the keypress event...
4
by: Rick | last post by:
I'm trying to make an input tag visible or hidden based on the value of another input tag, so that when the 1st input tag is changed in anyway, the 2nd input tag will become visible. Thanks in...
2
by: Sreenath Rao Nellutla | last post by:
Hai all, I am trying to create dropdown calendar control with HTML input control by writing JavaScript. But while executing I am getting the error as "Error on Page" on the status bar of the...
10
by: neverquit | last post by:
hi , Iam Nagesh,Begineer in using Ajax,well i have been using ajax in application, i have faced a problem while placing the responseTEXT into the <div> tag positioned inside the <form> tag iam...
2
by: jp2code | last post by:
I have several input fields on my form, and the form works; however, Visual Studio is showing errors, and I would like to get rid of them. When the form is submitted, it is redirected back to...
1
by: JPhilS | last post by:
Hi to all Webmaster! I have discover this problem when I inserted the scores of the students i a centain subject. I am making a school project with regards to saving students' record. first, I...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.