I have read another post/question regarding jquery variables and it was useful but I’m still having trouble understanding…
Below is a bit of code from a simple tabbed content functionality that I’m modifying.
Please could someone explain why this works:
$tabMenuItem.click(function() {
var activetab = $(this).find('a').attr('href');
$(activetab).show(); //show the active ID content
return false;
});
..and why this throws a ‘$activetab.show is not a function’ error:
$tabMenuItem.click(function() {
var $activetab = $(this).find('a').attr('href');
$activetab.show(); //show the active ID content
return false;
});
From what I can gather, both ways show the variable to hold the correct value – only the second version won’t let me attach a jquery method/function to it…?
I’m not a strong programmer, and any clarification would be appreciated!
activetabis thehrefof the selected anchor tag.show()is a jQuery method. The method needs to be called by a jQuery object.$(activetab)is a jQuery object. Albeit probably not what you want.If you wanted the second to work and using
$to prefix your variables, you still need to make it a jQuery object. You can do so by$($activetab).show().