sumit kang
sumit kang

Reputation: 476

how to delete input text box from javascript

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?

Upvotes: 1

Views: 5196

Answers (5)

Syed Ali Taqi
Syed Ali Taqi

Reputation: 4974

Try this:

var temp = document.getElementById('texts'+i);
           temp.parentNode.removeChild(temp);

Upvotes: 0

Bastien Durel
Bastien Durel

Reputation: 613

document.getElementById('frm').removeChild(document.getElementById('tbox'));

Upvotes: 1

Mayank Sharma
Mayank Sharma

Reputation: 829

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);
}

Upvotes: 0

AdityaParab
AdityaParab

Reputation: 7100

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);

Upvotes: 4

Taryn East
Taryn East

Reputation: 27747

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

Upvotes: 0

Related Questions