Checkbox that adds or removes textbox based on state

I'm trying to create a checkbox that will create a new textbox once clicked. Clicked again, it will remove the textbox. At the moment, it's just making a load of new textboxes as I don't know how to handle if statements inside of javascript. Can someone point me in the right direction please. Results
And the Javascript function CreateTextbox() { var textBox = document.createElement("input"); textBox.setAttribute("type", "textbox"); textBox.setAttribute("id", textboxId); textboxId++; return textBox; } var textboxId = 0; if(textboxId == 0) { document.getElementById("chk").onclick = function () { document.getElementById("formContainer").appendChild(CreateTextbox(textboxId)); var textboxId = 1; } } else if (textboxId == 1) { //The code to remove the previosuly made textbox }

以上就是Checkbox that adds or removes textbox based on state的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准