darksniperx Posted November 28, 2007 Share Posted November 28, 2007 <!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. Link to comment https://forums.phpfreaks.com/topic/79268-solved-cant-get-addremove-input-tag-to-work/ Share on other sites More sharing options...
kael.shipman Posted November 29, 2007 Share Posted November 29, 2007 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 https://forums.phpfreaks.com/topic/79268-solved-cant-get-addremove-input-tag-to-work/#findComment-401675 Share on other sites More sharing options...
darksniperx Posted November 29, 2007 Author Share Posted November 29, 2007 I have fixed it by doing this "userInputs_"+id Link to comment https://forums.phpfreaks.com/topic/79268-solved-cant-get-addremove-input-tag-to-work/#findComment-401677 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.