0
\$\begingroup\$

I am constructing a selector on every radio button click. Since I am using the table repeatedly on every radio click, I cached it like:

var $t1 = $("#tableone");

but inside the radio check event, I need to retrieve the selector to construct a string.

Approach 1:

$radio.click(function () {
        var temp = $t1.selector + " ." + $(this).attr("mobnum");

Note: If I do not $t1.selector, it comes as [object][Object] which I do not want, so I have to use $t1.selector.

Since I am using $t1.selector to construct temp every time radio is clicked, is there still a benefit caching the table at the beginning?

Approach 2:

$radio.click(function () {
var temp = $("#tableone") + " ." + $(this).attr("mobnum");

Which one's better?

\$\endgroup\$
1
  • \$\begingroup\$ It's unclear to me what you're doing. FYI, .selector is deprecated, and may be removed from jQuery in the future. \$\endgroup\$
    – cale_b
    Commented Oct 19, 2013 at 3:20

1 Answer 1

3
\$\begingroup\$

I don't think your approach 2 will do what you want. So I the first one would be better.

It looks to me though that you are building another selector. Probably to find the child element. So I would recommend something like this:

var $c = $t1.find("." + $(this).attr("mobnum")).

This returns the child element. This way you are already selecting only out of the children of $t1. Which would be more efficient, in theory.

\$\endgroup\$
2
  • \$\begingroup\$ So essentially now I can do operations on $c right. Like $($c).hide() or $($c).show() right? \$\endgroup\$
    – user1089173
    Commented Oct 19, 2013 at 3:33
  • \$\begingroup\$ That is correct, but you won't need to wrap it in a $(). $c.show() should be sufficient. \$\endgroup\$
    – ced-b
    Commented Oct 19, 2013 at 3:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.