Log in

View Full Version : Count Elements On Page



X96 Web Design
05-23-2009, 10:28 PM
Hi All,

I'm looking for a PHP or JavaScript script that can count all the elements on the page which have a certain class applied to them, and then spit out the number.

Thanks
X96

Medyman
05-24-2009, 08:26 PM
Simplest way I know is using jQuery (http://jquery.com/):


$(document).ready(function(){
var n = $('.classname').length;
});

X96 Web Design
05-25-2009, 12:04 AM
How would you echo n? I tried document.write(n) and it didn't work.

Thanks
X

Medyman
05-25-2009, 03:03 PM
Use the .html() (http://docs.jquery.com/Attributes/html) or the .text() (http://docs.jquery.com/Attributes/text) methods.