I saw some code around the web that uses the following statement
if ($($(this)).hasClass(\"footer_default\")) {
$(\'#abc\')
.appendTo($(this))
Yes, $($(this))
is the same as $(this)
, the jQuery()
or $()
function is wonderfully idempotent. There is no reason for that particular construction (double wrapping of this
), however, something I use as a shortcut for grabbing the first element only from a group, which involves similar double wrapping, is
$($('selector')[0])
Which amounts to, grab every element that matches selector
, (which returns a jQuery object), then use [0]
to grab the first one on the list (which returns a DOM
object), then wrap it in $()
again to turn it back into a jQuery object, which this time only contains a single element instead of a collection. It is roughly equivalent to
document.querySelectorAll('selector')[0];
, which is pretty much
document.querySelector('selector');