0

I have a table with rows that have the of custom attribute with each other. I want to hide all the table rows except for the parent row using jQuery (or Javascript). How can I go by doing this?

<table>
<tr group="1">Parent</tr>
<tr group="1">Child</tr>
<tr group="1">Child</tr>
<tr group="1">Child</tr>
</table>

Edit: Wow big typo on my part, I am terribly sorry, I meant custom attribute. Updated!

2
  • 4
    Actually, HTML with multiple elements having the same ID is invalid. Thus, your code is invalid. Commented Jun 16, 2011 at 22:14
  • Your HTML is still invalid. TR elements don't have a group attribute and can't have text as chlid nodes, they must have either TH or TD. Commented Jun 16, 2011 at 23:06

4 Answers 4

2

they can't have the same ID, the ID tag must be unique

you could simply use: $("table tr:gt(0)").hide()
(this only works if you don't have nested tables)

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

Comments

1

With your new example, it's possible using jQuery's atribute equals selector (here). Take a look at this tasty fiddle.

Basically, in your selector, you need this:

$("table tr[group='2']").hide()

Of course, this is customisable. The important bit is tr[group='2']

EDIT

This updated fiddle should work. If someone can post a better way, please do.

It adds to the above line with this:

$("table tr[group='2']").filter(":not(:first)").hide();

2 Comments

That's currently what I have, but I need to hide all except the parent row.
No problem :-) Glad I can be of assistance.
0

You can't have same id used twice on two HTML elements. Furthermore, you can't start an id with a number.

If you can change your HTML markup to use classes, which would look like this:

<table>
    <tr class="one">Parent</tr>
    <tr class="one">Child</tr>
    <tr class="one">Child</tr>
    <tr class="one">Child</tr>
</table>

Then, you would do it like this:

$('.one').hide();

Comments

0

You should not use IDs, only use classes when an element is repeated on the page, since ID refers to be unique, which can only be used once per page. Replace the id attribute to class and then you can use this code:

$('table tr[group=1]').hide();

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.