Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can’t I select a <td> using its class and data-test attributes in jQuery?

<table>
    <tr>
        <td class="ok" data-test="12-12 00">xxx</td>
        <td class="ok" data-test="13-12 00">xxx</td>
        <td class="ok" data-test="14-12 00">xxx</td>
        <td class="ok" data-test="15-12 00">xxx</td>
    </tr>
</table>

​I would like get the <td> where data-test = "14-12 00". Here’s my code:

alert($('td .ok[data-test="14-12 00"]').text());​

Why is this not working?

http://jsfiddle.net/LqD5h/

like image 595
Sean Marcoousin Avatar asked Dec 16 '22 17:12

Sean Marcoousin


2 Answers

Try:

alert($('td.ok[data-test="14-12 00"]').text());​

(Notice there is no space between td and .ok).

You were originally trying to select all elements with classname ok that are descendants of a td and bear a certain data-test value.

like image 162
Asad Saeeduddin Avatar answered Dec 18 '22 10:12

Asad Saeeduddin


How about another way to skin this cat?

alert($('tr .ok[data-test="14-12 00"]').text());​

Notice, td changed to tr. :-)

like image 38
Abhilash Avatar answered Dec 18 '22 09:12

Abhilash