最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dynamic - A button that when clicked will generate a new textbox? - Javascript - Stack Overflow

programmeradmin1浏览0评论

So, I have three textboxes. The first asks for a first name, the second for a last name, and the third for age.

When I click the button, a fourth textbox should be generated and it should contain everything that was entered in each textbox so far. My problem is I can't even get the fourth textbox to appear. I feel like I'm missing something that's pretty simple. Here is my code so far.

<html>

<head>
</head>

<body>

<script>
function addTextBox() {
var element = document.createElement("input");

element.setAttribute("type", "text");
element.setAttribute("value", "");
element.setAttribute("name", "Test Name");

}
</script>

<form>
<input type="text" value="Firstname">

<input type="text" value="Lastname">

<input type="text" value="Age">

<input type="button" onclick="addTextBox()">
</form>

</body>

</html>

So, I have three textboxes. The first asks for a first name, the second for a last name, and the third for age.

When I click the button, a fourth textbox should be generated and it should contain everything that was entered in each textbox so far. My problem is I can't even get the fourth textbox to appear. I feel like I'm missing something that's pretty simple. Here is my code so far.

<html>

<head>
</head>

<body>

<script>
function addTextBox() {
var element = document.createElement("input");

element.setAttribute("type", "text");
element.setAttribute("value", "");
element.setAttribute("name", "Test Name");

}
</script>

<form>
<input type="text" value="Firstname">

<input type="text" value="Lastname">

<input type="text" value="Age">

<input type="button" onclick="addTextBox()">
</form>

</body>

</html>
Share Improve this question asked Apr 1, 2015 at 4:36 LouLou 472 silver badges5 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

You need to append the element in body using appendChild whereas you have created element only missed to add it in document

function addTextBox() {
var element = document.createElement("input");

element.setAttribute("type", "text");
element.setAttribute("value", "");
element.setAttribute("name", "Test Name");
document.body.appendChild(element);
}
<html>
<head>
</head>
<body>
<script>
function addTextBox(btn) {
    var element = document.createElement("input");
    element.setAttribute("type", "text");
    element.setAttribute("value", "");
    element.setAttribute("name", "Test Name");
    btn.parentNode.insertBefore(element, btn)
}
</script>
<form>
<input type="text" value="Firstname">
<input type="text" value="Lastname">
<input type="text" value="Age">
<input type="button" id="btn" onclick="addTextBox(btn)">
</form>
</body>
</html>

an alternative would be to create the form and use css to hide it, then use your javascript function to display it and pump the data into it.

html

<form name="myForm">
    <input type="text" value="Firstname" />
    <input type="text" value="Lastname" />
    <input type="text" value="Age" />
    <input type="text" value="" name="Test Name" id="box4" style="display:none">
    <input type="button" onclick="addTextBox()" />
</form>

javascript

function addTextBox() {
    var box4 = document.getElementById("box4");
    box4.style.display = "inline";
    var myForm = document.forms['myForm'];
    for (var i = 0; i < myForm.elements.length; i++) {
        box4.value += myForm.elements[i].value + ",";
    }
}

JS fiddle

发布评论

评论列表(0)

  1. 暂无评论