Jump to content

onclick add new form field?


ag3nt42

Recommended Posts

I'm trying to create a button that will (once clicked)

 

create a new input form right beneath the first one..

 

but it doesn't seem to work for me.. the best I can get is it creates a whole new page and writes it there..

 

here is my code:

<form action='../Ca_info/Cct_info.php' method='post'>
				<table>
					<tr>
						<td style='text-align:right;'><label>Type: </label></td>
						<td style='text-align:left;'><input type='text' value='' name='Ctype[]' /></td>
					</tr>
					<script type='text/javascript'>

					function MakeInput()
					{
						document.write(\"<tr><td style='text-align:right;'><label>Type: </label></td><td style='text-align:left;'><input type='text' value='' name='Ctype[]' /></td></tr>\");
					}

					</script>
					<tr>
						<td colspan='2'><input type='button' value='ADD +' onclick='MakeInput();' /></td>
					</tr>
					<tr>
						<td> </td>
					</tr>
					<tr>
						<td style='text-align:right;'><input type='reset' value='Reset' /></td>
						<td style='text-align:left;'><input type='submit' value='Submit' /></td>
					</tr>
				</table>
			</form>

Link to comment
Share on other sites

I'm trying to create a button that will (once clicked)

 

create a new input form right beneath the first one..

 

but it doesn't seem to work for me.. the best I can get is it creates a whole new page and writes it there..

 

here is my code:

<form action='../Ca_info/Cct_info.php' method='post'>
				<table>
					<tr>
						<td style='text-align:right;'><label>Type: </label></td>
						<td style='text-align:left;'><input type='text' value='' name='Ctype[]' /></td>
					</tr>
					<script type='text/javascript'>

					function MakeInput()
					{
						document.write(\"<tr><td style='text-align:right;'><label>Type: </label></td><td style='text-align:left;'><input type='text' value='' name='Ctype[]' /></td></tr>\");
					}

					</script>
					<tr>
						<td colspan='2'><input type='button' value='ADD +' onclick='MakeInput();' /></td>
					</tr>
					<tr>
						<td> </td>
					</tr>
					<tr>
						<td style='text-align:right;'><input type='reset' value='Reset' /></td>
						<td style='text-align:left;'><input type='submit' value='Submit' /></td>
					</tr>
				</table>
			</form>

 

You're having problems because document.write() overwrites the current document.  If you want to create new elements, the safest way is to use the W3C functions, such as createElement(), createTextNode(), appendChild(), etc.

Link to comment
Share on other sites

i don't spose you could give me an example on how to generate a input and store in a variable or something so I can write it out to my page.. on the event?  :-\

 

It's pretty basic:

var newInput = document.createElement("input");
newInput.setAttribute("name", "userName");
newInput.setAttribute("type", "text");

form.appendChild(newInput); //assumes you have a variable named "form" that refers to an existing form.

/* should create <input name="userName" type="text"> */

Link to comment
Share on other sites

 

form.appendChild(newInput); //assumes you have a variable named "form" that refers to an existing form.

 

 

does this comment mean that I need to have a previous object created by js to do this or does it mean if I give  my form a name 'form' it will append an input onto that form?

Link to comment
Share on other sites

ok scratch the above.. its working .../sorta/

 

but its putting the new input on the bottom of the form underneath the submit buttons..

I need it under the first input

 

Hmm...I don't think there's a way to control where new elements will attach themselves while using appendChild.  It always puts the element at the end of the list of child elements.  I think there may be a prependChild function, but that would force the new element to be the first child element, which is also something you don't want.

 

You can try:

form.innerHTML = "stuff";

 

Where stuff is everything you want within your <form> tag.  This means, however, you'd have to rewrite your entire form, including the new input(s), as the "stuff" value.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.