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?

0

5 Answers 5

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);
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

2 Comments

... which won't work for browsers not implementing DOM level 4 (most IE versions)
...which wasn't specified in the question, but sure.
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);
}

Comments

0

Try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.