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?
1 Answer 1
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.
-
\$\begingroup\$ So essentially now I can do operations on $c right. Like $($c).hide() or $($c).show() right? \$\endgroup\$user1089173– user10891732013年10月19日 03:33:24 +00:00Commented 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– ced-b2013年10月19日 03:40:05 +00:00Commented Oct 19, 2013 at 3:40
.selector
is deprecated, and may be removed from jQuery in the future. \$\endgroup\$