Jump to content

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


darksniperx

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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>add/remove text boxes script</title>
</head>
<body>
<script type="text/javascript">
var userInput_boxes = 1;
function removeElement(id)
{
document.getElementById(userInput[id]).parentNode.removeChild(document.getElementById(userInput[id]));
}
function addElement()
{

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

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

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

    user.setAttribute("type","text");  
user.setAttribute("value","inputbox"); 
        user.setAttribute('name','userName[]');
user.setAttribute("id","userInput['userInput_boxes']");
    input.appendChild(user);
userInput_boxes = userInput_boxes + 1;
var a = document.createElement("a");
var id = userInput_boxes - 1;
//alert(id);
a.setAttribute("href","javascript:removeElement('id');");
aText = document.createTextNode("remove");
a.appendChild(aText);
input.appendChild(a);
 var br = document.createElement("br");
 input.appendChild(br);

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

 

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.