0

I have made elements using this function:

    var counterUpload = 1;
     var limit = 20;
     function addUpload(divName){
     if (counterUpload == limit)  {
          alert("You have reached the limit of adding " + counterUpload + " inputs");
     }
     else {
      var newdiv = document.createElement('p');
          newdiv.innerHTML = " <label>Offences: * </label><input required=\"required 
                              \"style=   \"width:670px;\"type=\"text\"
                               name=\"offences["+counterUpload+"]\" 
                               id=\"offences["+counterUpload+"]\"> "
      document.getElementById(divName).appendChild(newdiv);
      counterUpload++;
     }

}

here is my html

<fieldset class="ro5" id="ro5"> <legend>Add New:</legend> <p> <label>Offences: *  </label> <input name="offences" style="width:670px;" type="text" required="required"/>  </p>

divName takes ro5 as argument

this is what i have tried so far:

function removeUpload(divName){
if (limit==counterUpload)  {
  alert("You have reached the limit of removing " + counterUpload + " inputs");
}
else {
  var newdiv = document.removeElement('p');
  if(
  newdiv.innerHTML = " <label>Offences: * </label><input required=\"required\" style=\"width:670px;\"type=\"text\" name=\"offences["+counterUpload+"]\" id=\"offences["+counterUpload+"]\"> "){
  document.getElementById(divName).removeChild(newdiv);}
  counterUpload--;
}

}

from the two answers so far, i have managed to come up with this but still it doesnot get the job done,i feel very close yet far:

function removeUpload(divName) {
// Find the parent element
var parent = document.getElementById(divName);

if (parent) {
    // Find all the child nodes in parent element
    var children = parent.getElementsByTagName("P");
var num=count(children);
for(i = num; i >2 ; i--){
//i gave the P elements to remove an id="paraE" so that i do not remove other P elements //with out this id
    if( children[i].getAttribute('id') == 'paraE'){ 
        parent.removeChild(children[i]);
    } 
    }

}

how can I delete the elements?how can I reverse this function to do the opposite work???

  • Related http://stackoverflow.com/questions/3387427/javascript-remove-element-by-id – MightyPork Jul 07 '14 at 20:14
  • Does `divname` already have `

    ` children before your function starts?

    – PM 77-1 Jul 07 '14 at 20:20
  • thanks @andrew;please can you show me how to remove in this case:
    Add New:

    – user3253289 Jul 07 '14 at 20:23
  • @PM 77-1 above is the structure of my html before new elements are added. And divName takes ro5 as argument in this case – user3253289 Jul 07 '14 at 20:24
  • @user3253289 check the last part of my answer – hex494D49 Jul 07 '14 at 20:31
  • @user3253289 in your situation, your creating a DIV and then you are populating it with a label and a input element; finally appending to some other DIV. When we delte the first DIV we actually delete all what's inside. This is the last line of my answer – hex494D49 Jul 07 '14 at 20:34
  • And, in general you don't need an extra function to create or delete an element, kind of wrapper to something that's already self-describing. – hex494D49 Jul 07 '14 at 20:39
  • @user3253289 I updated the answer so you can see in steps how you can create form, it's elements and how can you delete all of them in one call. – hex494D49 Jul 07 '14 at 20:44
  • only `document.getElementById(divName).parentNOde.removeChild(document.getElementById(divName)); counterUpload--;` should be inside the `else` statement, if you want it this way :) – hex494D49 Jul 07 '14 at 21:27
  • @user3253289 sory :) no need for `parentNode` in this case; you managed to confuse me :) 'casue divName is the container in this case where all the p elements are – hex494D49 Jul 07 '14 at 21:32
  • no, no hahaha this is becoming funny; let me rewrite your function – hex494D49 Jul 07 '14 at 21:34

3 Answers3

0

Short answer

// create an element
var p = document.createElement("P");
p.innerHTML = "something";

// append to some other element
document.appendChild(p);

// delete it
p.parentNode.removeChild(p);

Don't do this

p.innerHTML = '<label>' + some_var + '</label>';

instead, create it in a proper way

var label = document.createElement("LABEL");
label.innerHTML = some_var;

The same applies to form elements

var form = document.createElement("FORM");
form.method = "post";
form.action = "some-url"; 

var input = document.createElement("INPUT");
input.type = "text";
input.name = "name";
input.value = ""; 

var button = document.createElement("INPUT");
button.type = "button";
button.name = "button";
button.value = "Submit"; 
button.onclick = function(){
    // do something
};

// append all form element to form and add the form to document
form.appendChild(input);
form.appendChild(button);
document.appendChild("form");

// want to delete all of it?
form.parentNode.removeChild(form);

Removing elements in more details

// Removing a specified element when knowing its parent node
var d = document.getElementById("top");
var d_nested = document.getElementById("nested");
var throwawayNode = d.removeChild(d_nested);

// Removing a specified element without having to specify its parent node
var node = document.getElementById("nested");
if (node.parentNode) {
    node.parentNode.removeChild(node);
}

// Removing all children from an element
var element = document.getElementById("top");
while (element.firstChild) {
    element.removeChild(element.firstChild);
}

YOUR SITUATION

// append it to DOM 
document.getElementById(divName).appendChild(newdiv);

// delete it / remove it from DOM
newdiv.parentNode.removeChild(newdiv);

Let's say, I'm using your function to create an element, and I have no clue how your function is buit inside. All I know is that your function creates an element and gives me the reference in return, So, I call it like this

// let's create a P element
var first_p = your_function('p'); 

// I have it now, and I can reach it by reference (first_p)
// if I want to delete it, I just call this
first_p.parentNode.removeChild(first_p);

// remove the n-th p element
function remove_p(n){
    var p = document.getElementsByTagName("P");
    for(i = 0; i < p.length; i++){
        if(p[i+1] == n){ 
            p[i].parentNode.removeChild(p[i]);
        } 
    }
}
hex494D49
  • 9,109
  • 3
  • 38
  • 47
  • is this the more proffessional way of creating the elements.It is new and looks cool to add to my learning .thanks for that +2. it means that p.innerHTML=label.innerHTML+input.______what here in this case?/ – user3253289 Jul 07 '14 at 20:34
  • @user3253289 this is a proper way how an element should be created or deleted from DOM, and yes, it looks cool :) – hex494D49 Jul 07 '14 at 20:36
  • hwat is in the

    is a label and an input.if I go likeso means that p.innerHTML=label.innerHTML+input.______, am I correct

    – user3253289 Jul 07 '14 at 20:44
  • P = paragraph, use it for text, label = for labeling form elements (in general) – hex494D49 Jul 07 '14 at 20:46
  • @user3253289 This is how you should use the label `` – hex494D49 Jul 07 '14 at 20:47
  • what i would like is to be able to delete the

    with all its contents and leave one

    inside the fieldset.say if i created 4 new

    that would be 5 in total,i would like to remove the 4 one by one as i click on a button,the opposite of the function above

    – user3253289 Jul 07 '14 at 20:49
  • @user3253289 the

    you won't to delete should be outside of this one you want to delete. Let's say your

    has `id=wow` all you have to do `onclick=document.getElementById("wow").parentNode.removeChild(document.getElementById("wow"));`

    – hex494D49 Jul 07 '14 at 20:54
  • @user3253289 but if you follow my answer, the statement above would be much shorter, 'cause you use reference to an element, not the whole (let's say) path. – hex494D49 Jul 07 '14 at 20:55
  • @user3253289 Ok I'll update my answer in a sec to make this much understandable. – hex494D49 Jul 07 '14 at 20:59
  • @494D49 please can you put that example to pratical in my case.as you can see from my code sample above,ican create a paragraph with a label and an input text box inside it not more than 20 times.now i would like some assistance to write the reverse of that function addupload().so that ican be able to delete the elements one by one and leave one . – user3253289 Jul 07 '14 at 21:04
  • @user3253289 Check the last part of the answer and say - I've got it :) – hex494D49 Jul 07 '14 at 21:05
  • @user3253289 Check the last function, the simplest possible :) The function will delete the n-th p, so if you want to delete the third one, just call it remove_p(3); – hex494D49 Jul 07 '14 at 21:16
  • luckily for you , you know the structure of the function because it is given.and iam using it like so please let us use our resources – user3253289 Jul 07 '14 at 21:28
0

Here's a second function that allows you to remove your child P elements.

function removeUpload(id, num) {
    // Find the parent element
    var parent = document.getElementById(id);

    if (parent) {
        // Find all the child nodes in parent element
        var children = parent.getElementsByTagName('P');

        // If the child exists, remove it
        // We have to subtract 1 because `getElementsyTagName` returns an array
        if (children && children[num - 1) {
            parent.removeNode(children[num - 1]);
        }
    }
}
rxgx
  • 5,089
  • 2
  • 35
  • 43
  • Thanks for your contribution +3.You are close to what i Seeketh.But the num variable??? these elements are going to be created dynamically so they could be 3,6 or 10.and iam calling this function like so: Can you get the total number of the newly created elements with num=count(parent.getElementsByTagName('P')); – user3253289 Jul 07 '14 at 21:40
  • @user3253289 var all = parent.getElementsByTagName('P') this way you'll get all p elements inside the parent element. – hex494D49 Jul 07 '14 at 21:52
  • @hex494D49 check ou my new update to what i have done so far,why is it not working – user3253289 Jul 07 '14 at 22:34
0
function removeUpload(element id) {
var myList =ro5.getElementsByTagName('P');//get total number of p elements available
// Find the parent element
var element = document.getElementById(element id);
var tot=myList.length-1;//subtract the one  P element without id="paraE"
if(tot>1)//make sure one p element remains visible always
{element.parentNode.removeChild(element);}
}

that did the trick. I based on the two answers from @rxgx,and @hex494D49:thanks plus this link:add/delete elements dynamically.And after a long while came up with the above and it is working..