If you are selecting your elements with jQuery, you can use length on your jQuery object.
var $foos = $("p > .foo");
// All jQuery objects have a length property.
alert($foos.length);
// Alternatively, you can use the size() method.
alert($foos.size());
<script type="text/javascript">
Array.prototype.isItAnArray = true; // give this property only to real arrays
function getLength(thing) {
if (typeof thing == "object" && !thing.isItAnArray) {
var count = 0;
for (var test in thing) {
count++;
}
return count;
} else {
return thing.length;
}
}
window.onload = function () {
var myArray = new Array(1,2,3,4,5);
var myObject = new Object();
myObject.test1 = 1;
myObject.test2 = 2;
myObject.test3 = 3;
myObject.test4 = 4;
myObject.test5 = 5;
alert(getLength(myArray));
alert(getLength(myObject));
}
</script>
It turns out that I incorrectly assumed that Internet Explorer included ’
’ newlines in a multi-line string. Now I am trying to figure out how to detect line breaks, any ideas?