I would like to know if it is possible to use eq() with filter(). I’ve already tested it and to no avail, though I’ve found a work around. I was just wondering in case I missed something.
My example is this…
Say you have a few tables with columns and rows.
<table class="menuTable"><tr><td>1</td><td>2</td></tr></table>
<table class="menuTable"><tr><td>3</td><td>4</td></tr></table>
I would like to find how many column spaces are in each table, so…
<script type="text/javascript">
alert($(".menuTable").eq(0).filter("td").length);
alert($(".menuTable").eq(1).filter("td").length);
</script>
This doesn’t work and I’d like to find out why. I ended up using .find() instead of .filter(), but I think it would be more appropriate to use .filter().
Thanks for any input.
After a few comments I realize my problem is in the difference between find and filter. Also… for those who are curious. I did manage to get it to work using filter by including eq in the selector…
alert($(".menuTable:eq(0)").filter("td").length);
findis indeed what you should be using here.filteras the name suggests, filters the collection of elements, in your case that collection is all.menuTableelements; there are notds in that collection.findon the other hand looks for children elements, and your collection does havetdchildren elements.