That's not something you should worry about unless dealing with thousands of non-existing elements. Looking for 100 inexistent IDs in a page takes less than 10ms - that time increases considerably for other selectors.
But that doesn't mean it's the right way to code it. You should have each page run only the functions it needs, if they can't be fired by an event you can test the window.location property to identify the page, or use a function call in each page's head. In the end you're not going to only look for the element, your code will try to run all the subsequent operations on the non-existent element. For best performance your code blocks should be called depending on your website/application's state, not blindly on every page load. - ricardo On Oct 9, 5:48 am, Micky Hulse <[EMAIL PROTECTED]> wrote: > Hi Michael! Thanks for your reply. :) > > > If your code runs any time after block of HTML e.g. in a "ready" function or > > even in inline code after the HTML, both the parentContianer [sic] and all > > of its child elements have already been created. Just grab your references > > and run. > > You advice makes a bunch of sense. Thank you for helping clarify. > > I guess I am still a little confused about when/if none of the > elements exist... > > For example, let's say I have a global JS file that is included on all > pages of my site, but let's say "#parentContainer", and all of its > children, only appear on the homepage (for example)... Would it add to > my JS overhead to have my global JS file always looking for elements > that only exist on the homepage (when not on the homepage)? > > I was thinking I could just look for the parent and not reference > children until the parent exists, in order to save on memory(?) > > ========================================= > > var $parentObj = $('#parentContianer'); > if($parentObj.length > 0) { > // $parentObj exists, cache child objects: > var $childObj1 = $('#childEle1'); > var $childObj2 = $('#childEle2'); > var $childObj3 = $('#childEle3'); > var $childObj4 = $('#childEle4'); > var $childObj5 = $('#childEle5'); > var $childObj6 = $('#childEle6'); > > } > > ========================================= > > But, then again, I am the student here (still waiting for my jQuery > reference book to arrive in the mail). :D > > Am I over-thinking this? Should I even worry about referencing objects > that do not exist? > > For example, if my master JS file did something like this: > > $(function() { > var $obj1 = $('#foo1'); > var $obj2 = $('#foo2'); > var $obj3 = $('#foo3'); > var $obj4 = $('#foo4'); > var $obj5 = $('#foo5'); > var $obj6 = $('#foo6'); > var $obj7 = $('#foo7'); > var $obj8 = $('#foo8'); > ... > ... > var $obj22 = $('#foo22'); > ... > ... > > }); > > Would it be sill for me to reference all those objects on every page, > even if I know that not every element is on all pages of my site? > > Pheew, I hope I am making sense here... Hrmmm, lol! Maybe I have been > driking too much coffee? > > Well, anyway... Many many thanks for the help Mike! I greatly > appreciate your time. :) > > Have a great day/night! > Cheers, > Micky