Problem with Add/remove elements

Discussion in 'JavaScript' started by arpit13, Aug 2, 2011.

  1. #1
    hi,

    i want two simple functions, one to add a input text field and the other to remove it dynamically.

    code i am using:
    
    
    var x=0;
    function add() {
    var dId = "div"+x;
    x++;
     document.getElementById('extra').innerHTML += "<div id=\'"+dId+"\'>URL:<input type='text' name='url[]' value=''><a href=\"javascript:delete('"+dId+"')\">X</a></div><br />";
    }
    function delete(div) {
    var d = document.getElementById('extra');
    var d2 = document.getElementById(div);
    d.removeChild(d2);
    }
    
    Code (markup):
    the problem is whenever i click the "X" in the generated field box, it opens in new window with some text saying "true", i guess it is an output for the function.
    i don't want that output.

    and i m using FF4 if it matters.
     
    arpit13, Aug 2, 2011 IP
  2. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #2
    "Delete" is a reserved word in Javascript. Choose another name for the function, like deleteNode
     
    Cash Nebula, Aug 3, 2011 IP
    arpit13 likes this.
  3. arpit13

    arpit13 Well-Known Member

    Messages:
    294
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    128
    Digital Goods:
    1
    #3
    hey thanks Cash Nebula, it worked.

    i added rep
     
    arpit13, Aug 4, 2011 IP