0

I am having problem using variables as selectors with jquery.

how do I select last td using $t?

<table id="mytable">
<tr>
<td>a</td>
<td>b</td>
</tr>
</table>

var $t = $('#mytable')

3 Answers 3

5

Use .find and combine with :last. This way you search the table for the last td

$t.find("td:last")

From jQuery docs:

.find

Description: Get the descendants of each element in the current set of matched elements, filtered by a selector, jQuery object, or element.

:last

Description: Selects the last matched element.

Note that :last selects a single element by filtering the current jQuery collection and matching the last element within it.

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

Comments

1

See working example here http://jsfiddle.net/MXjFP/2/

var $t = $("#mytable");
var $td = $t.find("td:last");
alert($td.html());

It's a little confusing and if at all possible I'd remove the '$' from your variable names to make it a little easier to read.

Comments

1

You can write

$t.find('td:last')

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.