Table row removal(NEW to this)

in Script development
Subscribe to Table row removal(NEW to this) 4 posts, 3 voices

SUPER_KEG Scriptwright

Hi Everyone,

I know that there is a few post already on row removal but have read and tried what they are saying but having no luck. But it may be because I am new.

Anyway what I am trying to do is remove the third row of a table.
The xpath /html/body/table/tbody/tr[3]
The HTML code is

< body id="slam">
   < table id="jam">
       <tr>  1  </tr>
       <tr>  2  </tr>
       <tr>  3  </tr>
   < /table>

what I have tried is



document.evaluate('//table/tbody/tr[3]',document,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null).style.display = 'none';

Any help would be greatly appreciated.

devnull69 Scriptwright

It's getElementById with no "s" in it. It returns exactly one DOM element. Then you spelled getElementsByTagName correctly, but the parameter is supposed to be a string "tbody" and it returns a collection of DOM elements. If you want to address a specific one (e.g. the first) you'll have to use a proper index on the collection

var theParent = document.getElementById('jam').getElementsByTagName('tbody')[0];

Monkey2000 User

For what it's worth, a table also has a rows collection. If there are no rows in a <thead> element, then you should be able to remove the third row in the table's rows collection like this:

rowToDelete = document.getElementById("jam").rows[2];

SUPER_KEG Scriptwright

Thanks devnull69 for pointing out the 's' also got it working by


Thanks to Monkey2000 point out can just use the table's id

Thanks both for the help