Jump to content

[SOLVED] cant get add/remove input tag to work


Recommended Posts

<!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">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>add/remove text boxes script</title>
<script type="text/javascript">
var userInput_boxes = 1;
function removeElement(id)
function addElement()

var input = document.getElementById("name");

    var user = document.createElement("input");           

    //var cellText = document.createTextNode("only text ids display, no innerHTML?");

userInput_boxes = userInput_boxes + 1;
var a = document.createElement("a");
var id = userInput_boxes - 1;
aText = document.createTextNode("remove");
 var br = document.createElement("br");

<form action="" method="post">
<a href="javascript:addElement();">add</a>
<div id="name">


add input code tag works, it is remove input tag that gives trouble, I think something is setAttribute, but I cant figure out.


Also i would like to know if I would submit the page would it submit the newly create input tags as well for me to work with.

Link to comment
Share on other sites

To the first question: you have to quote the entire id. You're trying to access a non-existent javascript userInput array, when you should be passing the string "userInputs["+id+"]" to the getElementById() function. As a side note, you don't need the single quotes in the input name, either ("userInput['userInput_boxes']" should be "userInput[userInput_boxes]").


To the second question: Yes, all inputs on the page, whether static or dynamic, will be available so long as they are children of the <form> element that's being submitted.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

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

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.