0

Just want to know how to select a table row by id

eg the table row has the id of "50"

...

How can I select this (as I wish to use the hide() function on a click)

2
  • Almost got it.. This is my code $('.remove').click(function () { var row = ''; row = $(this).attr('id'); console.log(row); $('tr#'.row).hide(); }); Commented Sep 15, 2011 at 12:07
  • Got it - thanks guys $('.remove').click(function () { var row = ''; row = $(this).attr('id'); console.log(row); $('tr#'+row).hide(); }); Commented Sep 15, 2011 at 12:10

5 Answers 5

2
$('tr#50').click(function(ev) { $(this).hide(); });

$('tr#50') selects the row. .click(function(){...}) runs the function when you click on the row. $(this) is a way of selecting the original element that you set the click handler on (in this case, that selected by "tr#50"). And .hide(), obviously, hides that element.

EDIT: As other answerers point out, it's not good practice to start an id with a number, and even worse practice to make your id only a number. You should rename it to something like row-50.

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

Comments

1

write this on Click event

 $('tr#50').hide()

But i would suggest you that don't start your id with Number as it's not a good practice ...

Comments

0
$('tr#50')

be careful with number-only ids though

Comments

0

To select an element with an id just use the following selector:

$("tr#id").hide();

So for your problem with the id 50 it would be:

$("tr#50").hide();

Comments

0

If you don't have an ID of the table and simply wants to select the 50th row, you can select it with:

$('table tr').eq(49);

or

$('table tr:eq(49)');

Note that eq() starts on 0, and not 1. There for the eq() in this case is (50 - 1)

2 Comments

When we are having id, then why you are going for eq..it will be much slower
I felt the question to be too simple to mean the id="" attribute, and therefor drew the conlusion the questioner may have meant this instead. I've also read that id="" shouldn't start with a number.

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.