I have the following javascript that dynamically add textboxes together with a remove button to a div. How can I using the remove button delete the content of the row being selected to delete?
<script type="text/javascript">
function GetDynamicTextBox(value) {
return('' +
'<div class="col-md-2">' +
' <input type="text" name="events[0].Key" value=""/>' +
'</div>' +
'<div class="col-md-2">' +
' <input type="text" name="events[0].Value.StartDate" value=""/>' +
'</div>' +
'<div class="col-md-2">' +
' <input type="text" name="events[0].Value.EndDate" value=""/>' +
'</div>' +
'<div class="col-md-2">' +
' <input type="text" name="events[0].Value.Description" value=""/>' +
'</div>' +
'<div class="col-md-2">' +
' <button type="button" class="btn btn-sm btn-primary" onclick="RemoveTextBox(this)"><i class="fa fa-angle-right"></i> Remove</button>' +
'</div>');
}
function AddTextBox() {
var div = document.createElement('DIV');
div.className = "form-group";
div.innerHTML = GetDynamicTextBox("");
document.getElementById("divcontent").appendChild(div);
}
function RemoveTextBox(div) {
//document.getElementById("divcontent").removeChild(div.parentNode); // this does not work
iterateBoxesAndSetUniqueIds();
}
function iterateBoxesAndSetUniqueIds() {
// Set unique names of each textbox
var children = document.getElementById("divcontent").children;
for (i = 0; i < children.length; i++)
{
var el = children[i];
el.name = 'events[' + i + '].Key'; //
el.id = 'events[' + i + '].Key';
el.name = 'events[' + i + '].Value.StartDate';
el.id = 'events[' + i + '].Value.StartDate';
el.name = 'events[' + i + '].Value.EndDate';
el.id = 'events[' + i + '].Value.EndDate';
el.name = 'events[' + i + '].Value.Description';
el.id = 'events[' + i + '].Value.Description';
}
}
</script>
<button type="button" class="btn btn-sm btn-primary" onclick="AddTextBox()"><i class="fa fa-angle-right"></i> Add</button>
<div id="divcontent">
</div>
UPDATE I added a snippet for updating the id for each textbox, but obviously I am doing something wrong here. Can anyone help with this too?
I assume you want to remove the entire row, so heres how to do that.
You almost had it, since you pass (this)
in the onclick
, it means we have a direct reference to the button that was clicked. From there, we can get its grandparent by using .parentNode
twice (since the first parent is .col-md-2
) and then use the remove()
function.
div.parentNode.parentNode.remove()
function GetDynamicTextBox(value) { return('' + '<div class="col-md-2">' + ' <input type="text" name="events[0].Key" value=""/>' + '</div>' + '<div class="col-md-2">' + ' <input type="text" name="events[0].Value.StartDate" value=""/>' + '</div>' + '<div class="col-md-2">' + ' <input type="text" name="events[0].Value.EndDate" value=""/>' + '</div>' + '<div class="col-md-2">' + ' <input type="text" name="events[0].Value.Description" value=""/>' + '</div>' + '<div class="col-md-2">' + ' <button type="button" class="btn btn-sm btn-primary" onclick="RemoveTextBox(this)"><i class="fa fa-angle-right"></i> Remove</button>' + '</div>'); } function AddTextBox() { var div = document.createElement('DIV'); div.className = "form-group"; div.innerHTML = GetDynamicTextBox(""); document.getElementById("divcontent").appendChild(div); } function RemoveTextBox(div) { div.parentNode.parentNode.remove() }
<button type="button" class="btn btn-sm btn-primary" onclick="AddTextBox()"><i class="fa fa-angle-right"></i> Add</button> <div id="divcontent"> </div>
function GetDynamicTextBox(value, uniquedId) { return('' + '<div class="col-md-2">' + ' <input type="text" name="events[0].Key" value=""/>' + '</div>' + '<div class="col-md-2">' + ' <input type="text" name="events[0].Value.StartDate" value=""/>' + '</div>' + '<div class="col-md-2">' + ' <input type="text" name="events[0].Value.EndDate" value=""/>' + '</div>' + '<div class="col-md-2">' + ' <input type="text" name="events[0].Value.Description" value=""/>' + '</div>' + '<div class="col-md-2">' + ' <button type="button" class="btn btn-sm btn-primary" onclick="RemoveTextBox(\'' + uniquedId +'\')"><i class="fa fa-angle-right"></i> Remove</button>' + '</div>'); } function AddTextBox() { var uniquedId = Math.floor(Math.random() * 100) + '_unique'; var div = document.createElement('DIV'); div.setAttribute("id", uniquedId); div.className = "form-group"; div.innerHTML = GetDynamicTextBox("", uniquedId); document.getElementById("divcontent").appendChild(div); } function RemoveTextBox(uniquedId) { var elem = document.getElementById(uniquedId); return elem.parentNode.removeChild(elem); }
<button type="button" class="btn btn-sm btn-primary" onclick="AddTextBox()"><i class="fa fa-angle-right"></i> Add</button> <div id="divcontent"> </div>
<script type="text/javascript">
function GetDynamicTextBox(value, uniquedId) {
return('' +
'<div class="col-md-2">' +
' <input type="text" name="events[0].Key" value=""/>' +
'</div>' +
'<div class="col-md-2">' +
' <input type="text" name="events[0].Value.StartDate" value=""/>' +
'</div>' +
'<div class="col-md-2">' +
' <input type="text" name="events[0].Value.EndDate" value=""/>' +
'</div>' +
'<div class="col-md-2">' +
' <input type="text" name="events[0].Value.Description" value=""/>' +
'</div>' +
'<div class="col-md-2">' +
' <button type="button" class="btn btn-sm btn-primary" onclick="RemoveTextBox(\'' + uniquedId +'\')"><i class="fa fa-angle-right"></i> Remove</button>' +
'</div>');
}
function AddTextBox() {
var uniquedId = Math.floor(Math.random() * 100) + '_unique';
var div = document.createElement('DIV');
div.setAttribute("id", uniquedId);
div.className = "form-group";
div.innerHTML = GetDynamicTextBox("", uniquedId);
document.getElementById("divcontent").appendChild(div);
}
function RemoveTextBox(uniquedId) {
var elem = document.getElementById(uniquedId);
return elem.parentNode.removeChild(elem);
}
</script>
<button type="button" class="btn btn-sm btn-primary" onclick="AddTextBox()"><i class="fa fa-angle-right"></i> Add</button>
<div id="divcontent">
</div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.