Search code examples
jqueryjquery-selectorsjquery-filter

jQuery selector for first appearance of span in td


I have the following HTML

<table>
    <tr>
        <td><span></span></td>
        <td><a><span></span></a></td>
        <td><span></span><span><span/></td>
    </tr>
<table/>

I need to select the first appearance of span in each td. I would exepcted 3 spans from the above HTML.

$('td span') returns all (4) spans

$('td span:first-child') return all spans also

$('td span:first') return only first

$('td > span') return only 2 spans.

Could I do this by selector or filter?
What do I have to use to achieve this?


Solution

  • Try this:

    ​$('td').find('span:first')
    

    http://jsfiddle.net/qDAt8/

    Please note that your markup is not valid.