0

I'm working on this HTML Page where i can add a check-box along with the label by clicking the add button, is there anyway that i can have a delete button as well so that when i select a check-box and press the delete button the check-box along with the text box is deleted??Please find my code below!!

<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function Add()
{
var checkbox=document.createElement('input');
var inps=document.createElement('input');
var output=document.getElementById('output');
checkbox.type='checkbox';
inps.type='text';
inps.name='textboxname';
checkbox.name='checkname';
output.appendChild(checkbox);
output.appendChild(inps);
output.appendChild(document.createElement('br'));
}
</script>
</head>
<body>
<form action="http://localhost:9990" method="post">
<span id="output"></span>
<input type="button" value="Add" onclick="Add()">
<center>
<p>
<input type="file" name="datafile" size="40">
</p>
<div>
<input type="submit" value="Send">
</div>
</center>
</form>
</body>
</html>
5
  • can we see your try code to help you ? or provide jsfiddle ? Commented Oct 29, 2013 at 5:08
  • Post you code,better make a fiddle Commented Oct 29, 2013 at 5:09
  • possible duplicate of Remove element from DOM Commented Oct 29, 2013 at 5:11
  • my try code is present above Commented Oct 29, 2013 at 5:16
  • @Santosh459 updated answer based on your requirement. check please Commented Oct 29, 2013 at 5:37

3 Answers 3

1

Give all of the check boxes that could potentially be deleted the same class. Additionally label all of the text boxes that could be deleted with their own class. My check boxes will be labeled with chk and my text boxes will be labeled with txt. As in:

<input type="checkbox" class = 'chk' /> and
<input type="text" class = 'txt' />

The following solution should work as long as check boxes and text fields are 1 to 1.

the function you will add to your delete button will loop through all of the check boxes and see if they are deleted and then delete the checked ones.

Heres the JS:

function delBoxes(){
    var boxes = document.getElementsByClassName('chk');
    var texts = document.getElementsByClassName('txt');
    for(var i = 0; i<boxes.length; i++){
        box = boxes[i];
        txt = texts[i];
        if(box.checked){
            box.parentNode.removeChild(box);
            txt.parentNode.removeChild(txt);
        }
    }
}

That will delete all of the checked check boxes, all you have to do now is make a button and add that function as an onclick.

<input type="button" value="Delete checked boxes" onclick = "delBoxes();" />
Sign up to request clarification or add additional context in comments.

Comments

1

Sample HTML

<div>
    <input type='checkbox' value='asd' id='test' name='test' />
    <input type='checkbox' value='asd' id='tester' name='test' />
    <input type='button' value='remove' id='rmvBtn' />
</div>

In pure javascript, you can do this,

var rmvBtn = document.getElementById('rmvBtn');

rmvBtn.addEventListener('click', function(){
   var rmvCheckBoxes = document.getElementsByName('test');

    for(var i = 0; i < rmvCheckBoxes.length; i++)
    {
        if(rmvCheckBoxes[i].checked)
        {
            removeElm(rmvCheckBoxes[i]);    
        }
    }  
});

function removeElm(elm){
   elm.parentElement.removeChild(elm);
}

JS Fiddle

Comments

0

HTML

<input type="checkbox" id="checkboxid" name="checkboxname">
 <button id="btnDeleteid" onclick="deleteCheckBox()" name="btnDeletename">Delete</button>

JavaScript

function deleteCheckBox(){
  if (document.getElementById('checkboxid').checked){
        var answer = confirm('Are you sure you want to delete this checkbox?');
         if (answer)
           {
           $("#checkboxid").remove();
           }
         }else{
           alert("Pls check the checkbox.");
          }
}

I hope this will help.

Refer fiddle - http://jsfiddle.net/F8w8B/3/

2 Comments

Hi hariharan,there is a provision for adding multiple checkboxes in this code can i delete all of them by checking on them and clicking the delete button??
yes, if you are able to get the refer of the component, then you can able to delete it all. place a parent div and add all the checkbox inside that div, if you like to delete all. call that parent div and remove it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.