2

I want to delete the input elements from the table. I could not figure out any way to delete them via JavaScript.

<table><tbody><tr>
<td><input id="c11" value=" " size="1" style="border-width: 0px;" type="text"></input></td>
<td><input id="c12" value=" " size="1" style="border-width: 0px;" type="text"></input></td>
</tr>
</tbody>
</table>
2
  • 1
    Plain vanilla JavaScript or jQuery? Commented Dec 12, 2012 at 16:47
  • possible duplicate of JavaScript: remove element by id Commented Dec 12, 2012 at 16:56

6 Answers 6

1

You can do that :

var inputs = document.getElementsByTagName('input');
while (inputs.length) inputs[0].parentNode.removeChild(inputs[0]);

If you have your table element, you could also use myTable.getElementsByTagName('input');.

Demonstration

Sign up to request clarification or add additional context in comments.

3 Comments

Yes. Maybe I should have precised that you can't loop in the other direction as the list is updated dynamically.
This doesn't solve the problem though. He wanted only the inputs in the table, and technically this would remove ALL inputs on the page, so assuming that this is only an excerpt and not the full html, that would possibly be bad.
@KyleRichter Maybe you should also read the last line of my answer.
1
document.getElementById('c11').outerHTML =  "";

Comments

0

if your using Jquery

Use .remove()

$("#yourtableid tr").remove();

If you want to keep the data for future use even after removing it then you can use .detach()

$("#yourtableid tr").detach();

Comments

0

It's work!

document.getElementById('c11').val("");
document.getElementById('c12').val("");

Comments

0

Try this code

 function removeRow(r)
    {
         var fo = "foo";
         var ido = r.id.substring(3,r.id.length-1);
         for(i=1;i<=3;i++)
         {
            document.getElementById(fo+id0+i).remove();
         }
         document.getElementById(fo+ido+5).nextSibling.remove();  
         document.getElementById(fo+ido+5).remove();
    }

Comments

-1

Personally, if you don't have access to jQuery, I would just give the parent element an id and then do this

var els = document.findElementById('tableId').childNodes.getElementByTagName('input');
for( var i = 0; i < els.length; i++){
    els[i].removeNode(true);
}

Alternately, you could substitute the code with something like this for complete browser compatibility, as @dystroy pointed out removeNode is only available in IE.

var els = document.getElementById('tableId').getElementsByTagName('input');
while( els.length ){
    els[0].parentNode.removeChild(els[0]);
}​

Something like that should work. Honestly, I would use jQuery. Much easier.

7 Comments

removeNode only exists on IE.
Even more reason to use jQuery's remove() method. :)
jQuery is very often useful but you don't import a library like jQuery just to avoid typing two lines of code (see my answer). This only can't be a reason.
This will fail. You're performing forward iteration through a live list that is being modified. Every time you remove an element, the list is reindexed from that point forward.
You're absolutely right. I modified it slightly. It's degrading in what I would consider a good solution, though. The goal was not to give an exact code example, rather show him how he might think through it logically to solve his problem, giving some javascript methods which would allow him to do that.
|

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.