mjmitche
mjmitche

Reputation: 2067

JavaScript querySelectorAll

A tutorial I'm working on has the following code with the following comment. I don't understand...

i) the comment, specifically, why it says "all second table cells." What does "second" mean? This isn`t proper English

ii) what is it looking for exactly when it says td + td? The program is about taking data from a table, so would td + td pick out anything between html table tags <td></td> for example?

//use querySelector to find all second table cells
var cells = document.querySelectorAll("td + td");

Upvotes: 2

Views: 2926

Answers (2)

Luke
Luke

Reputation: 1233

It's looking for only <td>s which are preceded by another <td> ( http://www.w3.org/TR/CSS2/selector.html#adjacent-selectors ).

So with this HTML, it would match <td>s 2-4 (inclusive):

<table>
    <tr>
        <td>Cell 1</td>
        <td>Cell 2</td>
        <td>Cell 3</td>
        <td>Cell 4</td>
    </tr>
</table>

An example here: http://jsfiddle.net/tMrbA/

Upvotes: 5

ThiefMaster
ThiefMaster

Reputation: 318738

If you have a selector foo + bar, it will search for an element foo and then select all elements matching bar which are siblings coming after foo and have the same parent as foo.

Upvotes: 1

Related Questions