1

i have created a input text box from javascript with the following code in runtime

var textbox = document.createElement('input');

textbox.type = 'text';

textbox.id='tbox';
textbox.value=document.getElementById("texts"+i).value;

document.getElementById('frm').appendChild(textbox);

How can i delete the same textbox in runtime?

CodingIntrigue
  • 71,301
  • 29
  • 165
  • 172
sumit kang
  • 412
  • 7
  • 19

5 Answers5

4

In javascript, you can not directly remove the element. You have to go to its parent element to remove it.

var elem = document.getElementById("tbox");
elem.parentNode.removeChild(elem);
AdityaParab
  • 6,601
  • 3
  • 24
  • 36
1
document.getElementById('frm').removeChild(document.getElementById('tbox'));
Bastien Durel
  • 583
  • 4
  • 18
0

try remove()

so assuming I've got the right one:

document.getElementById("texts"+i).remove()

If not the above... then make sure you give it an id and choose it by that before remove()ing it

Taryn East
  • 26,730
  • 9
  • 86
  • 103
0

Possible duplicate: Remove element by id

However you can use the following solution:

You could make a function that did the removing for you so that you wouldn't have to think about it every time.

function remove(id)
{
    return (elem=document.getElementById(id)).parentNode.removeChild(elem);
}
Community
  • 1
  • 1
Mayank Sharma
  • 856
  • 7
  • 21
0

Try this:

var temp = document.getElementById('texts'+i);
           temp.parentNode.removeChild(temp);
Syed Ali Taqi
  • 4,658
  • 2
  • 32
  • 44