You can short it to: $(function() { var divcount = $("div").length; alert("start=" + divcount); });
On Jan 19, 4:55 am, Karl Rudd <karl.r...@gmail.com> wrote: > The code is being run before the DIVs actually exist. You need to > "wrap" the code in a "ready" event handler, like so: > > $(document).ready( function() { > var divcount = $("div").length; > alert("start=" + divcount); > > }); > > Now the code will be run when the document (the HTML not images, etc) > has finished loading and is "ready". > > More info about the "ready" function/event here: > > http://docs.jquery.com/Events/ready#fn > > Karl Rudd > > > > > > On Mon, Jan 19, 2009 at 12:45 PM, bartee <bar...@gmail.com> wrote: > > > I have this test code. My alert dialog always show zero as the div > > count: > > > Help !!!!!!! > > > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" > > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> > > <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en-AU"> > > <head> > > <title>Jquery Test</title> > > <script type="text/javascript" src="jquery-1.3.js"></script> > > <script type="text/javascript"> > > var divcount = $("div").length; > > alert("start=" + divcount); > > </script> > > </head> > > <body> > > <div id="content"> > > <div id="div2">asdf</div> > > <div id="div2">asdf</div> > > </div> > > </body> > > </html>