Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

html - How to Delete the Textbox dynamically in a DIV using javascript

How to Delete the TextBox in a DIV using javascript. First I created the four column in a div and I created the checkbox dynamically using the Add button in a div.

What is my problem is I need to delete the appropriate textbox using the Delete button (behind the textbox).

So please give me a solution to rectify it.

function Add() {
  div1.innerHTML += "<br><br> <input type='text' name='mytext'>" + "<input type='button' value='Delete' onclick='removeRow(this)'>";
  div2.innerHTML += "<br><br> <input type='text' name='mytext'>" + "<input type='button' value='Delete' onclick='removeRow(this)'>";
  div3.innerHTML += "<br><br> <input type='text' name='mytext'>" + "<input type='button' value='Delete' onclick='removeRow(this)'>";
  div4.innerHTML += "<br><br> <input type='text' name='mytext'>" + "<input type='button' value='Delete' onclick='removeRow(this)'>";
}

function removeRow(input) {
  document.getElementById('div1').removeChild(input.parentNode);
}
#Wrapper {
  width: 90%;
  margin: 0 auto;
}
#div1 {
  float: left;
  width: 20%;
  background: lightblue;
}
#div2 {
  float: left;
  width: 20%;
  background: lightyellow;
}
#div3 {
  float: left;
  width: 20%;
  background: lightgray;
}
#div4 {
  float: left;
  width: 20%;
  background: lightblue;
}
#ClearFix {
  clear: both;
}
<button onclick="Add()">Add TextBox</button>
<div id="Wrapper">
  <div id="div1">
    <p>This is Div one</p>
  </div>

  <div id="div2">
    <p>This is Div two</p>
  </div>

  <div id="div3">
    <p>This is Div threee</p>
  </div>

  <div id="div4">
    <p>This is Div Four</p>
  </div>

  <div id="ClearFix"></div>
</div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this working snippet.

// Code goes here

function Add() {
  div1.innerHTML += "<br><br> <input type='text' name='mytext'>" + "<input type='button' value='Delete' onclick='removeRow(this)'>";
  div2.innerHTML += "<br><br> <input type='text' name='mytext'>" + "<input type='button' value='Delete' onclick='removeRow(this)'>";
  div3.innerHTML += "<br><br> <input type='text' name='mytext'>" + "<input type='button' value='Delete' onclick='removeRow(this)'>";
  div4.innerHTML += "<br><br> <input type='text' name='mytext'>" + "<input type='button' value='Delete' onclick='removeRow(this)'>";
}

function removeRow(input) {
  if(input.previousElementSibling.tagName == "INPUT"  && input.previousElementSibling.getAttribute("type") == "text") {
    var inputElement = input.previousElementSibling;
    var divId = input.parentNode.id;
    document.getElementById(divId).removeChild(inputElement);
  }
}
/* Styles go here */

#Wrapper {
  width: 90%;
  margin: 0 auto;
}
#div1 {
  float: left;
  width: 20%;
  background: lightblue;
}
#div2 {
  float: left;
  width: 20%;
  background: lightyellow;
}
#div3 {
  float: left;
  width: 20%;
  background: lightgray;
}
#div4 {
  float: left;
  width: 20%;
  background: lightblue;
}
#ClearFix {
  clear: both;
}
<!DOCTYPE html>
<html>



<body>
  <h1>Hello Plunker!</h1>
  <button onclick="Add()">Add TextBox</button>

  <div id="Wrapper">
    <div id="div1">
      <p>This is Div one</p>
    </div>

    <div id="div2">
      <p>This is Div two</p>
    </div>

    <div id="div3">
      <p>This is Div threee</p>
    </div>

    <div id="div4">
      <p>This is Div Four</p>
    </div>

    <div id="ClearFix"></div>
  </div>

</body>

</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...