I do this (its working fine I assign width amongst other things to all anchors) :
var anchors = jQuery("#myDiv a");
However I then want to select a specific anchor from the var, this doesn’t work :
anchors("#anchor01").addClass("myClass");
and neither does this :
anchors.find("#anchor01").addClass("myClass");
I realize I could do jQuery("#anchor01") but I figure its quicker to select from the already reduced elements stored in var. This must be so obvious …
Use the
filter()[docs] method.Of if you didn’t mind a little departure from jQuery, you could use
getElementsByTagName(), and access the element with the ID as a property of theNodeList.Example: http://jsfiddle.net/Tphfb/
EDIT:
Using the
NodeListis by far the fastest. Here’s a jsPerf.Plus, because it’s a “live list” it will automatically update when anchors are added to or removed from
myDiv.