views:

129

answers:

3

I have a <table id="myTable"> with a number of child elements:

<tr><td>
   <span class="mySpan" />
</td></tr>
<tr><td>
   <span class="mySpan" />
</td></tr>
<tr><td>
   <span class="mySpan" />
</td></tr>

How to select all this spans using jQuery?

+2  A: 
$('#myTable span.mySpan')
Darin Dimitrov
+1  A: 

"Safest" version:

var spans = $("#myTable tr td span.mySpan");

Shortest version:

var spans = $("#myTable .mySpan");
Vincent
shortest would be $(".mySpan"), but adding the #myTable should be faster as it starts at the table and not the document root.
Mark Schultheiss
@Mark Schultheiss but you can know if there are other spans in the document that match .mySpan :)
Vincent
Yes, hence my explaination (faster, more precise with #myTable :) I do agree your answers are NOT "wrong" either :)
Mark Schultheiss
+2  A: 

Note that you COULD do any of the following:

$("#myTable").find(".mySpan");
$("#myTable .mySpan");
$("#myTable tr td .mySpan");
$("#myTable").find("tr").find("td").children(".mySpan");
$(".mySpan");
$("#myTable > tr > td > .mySpan");
$(".mySpan ,#myTable");
Mark Schultheiss