0

I have a problem removing a row with JavaScript, since it does not have a class nor an ID, I can not reference it with CSS.

The row I'd like to eliminate begins with the text: Post

Hope someone can help

3
  • 2
    Can you post your code, or even better, make a demo for us on JsFiddle? Commented Mar 2, 2011 at 21:46
  • You'll need to provide more information than that. Also, your topic says "Java" which is incorrect. Another question is how do you want to remove it. Is there an Enter button somewhere? Do you click on a link? Do you have the code available of what you want to do? Commented Mar 2, 2011 at 21:47
  • Your question is extremely vague, we can't make sense of it. Also, is a jQuery solution acceptable or can you not use it? Commented Mar 2, 2011 at 22:01

1 Answer 1

4

This does it:

var rows = document.getElementById('theTable').rows;

for (var i = 0; i < rows.length; i++) {
   if ( rows[i].firstElementChild.textContent.trim().split(' ')[0] === 'Post' ) {
       rows[i].parentNode.removeChild(rows[i]);
   }  
}

Note: trim() and what not does not work in IE8. You can leave it out, but then you have to make sure that there is no leading white-space before the word "Post".

Live demo: http://jsfiddle.net/simevidas/xn6U8/


Update:

var rows = document.getElementsByClassName('basket')[0].rows;

for (var i = 0; i < rows.length; i++) {
   if ( rows[i].cells[0].textContent.trim().split(' ')[0] === 'Post' ) {
       rows[i].parentNode.removeChild(rows[i]);
   }  
}

Live demo: http://jsfiddle.net/simevidas/A73PK/2/

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

12 Comments

This the correct way too, split(' ')[0] === 'Post' checks the beginning
hmm! I can not get it to work - here is the entire table <table class="basket" border="0" style="width:100%;"> <tr> <td colspan="3">&nbsp;</td> </tr><tr> <td class="name">Post Danmark eller GLS takster</td><td class="price-right" align="right">0,00%</td> <td class="price-right" align="right">0,00 DKK</td> </tr><tr> <td>Faktura (Total m/moms)</td><td class="price-right" align="right">0,00%</td> <td class="price-right" align="right">0,00 DKK</td> </tr> </table>
@Pete In my demo the table has an ID and I use getElementById to select it. Your table does not have an ID. You have to either define an ID, or use another means to select the table element. But my code works for your table. See here: jsfiddle.net/simevidas/A73PK
@Šime: I'd just note that firstElementChild and textContent aren't supported in IE before IE9. For firstElementChild, you can use .cells[0] instead to get the first cell in the row.
But the problem is, I can't change the Code, can't I remove the table when it has a class=
|

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.