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
  • 75,930
  • 30
  • 170
  • 176
sumit kang
  • 456
  • 1
  • 7
  • 20

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
  • 7,024
  • 3
  • 27
  • 40
1
document.getElementById('frm').removeChild(document.getElementById('tbox'));
Bastien Durel
  • 603
  • 5
  • 20
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
  • 27,486
  • 9
  • 86
  • 108
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
  • 844
  • 7
  • 21
0

Try this:

var temp = document.getElementById('texts'+i);
           temp.parentNode.removeChild(temp);
Syed Ali Taqi
  • 4,898
  • 3
  • 34
  • 44