Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selector: finding table cell based on content

I'm trying to select a table cell which contains specific text but couldn't find out yet how it works: I'm having this code - but doesn't work:

var td = $("td:contains('MyCell')",tbl);

Any ideas what's wrong?

like image 918
Fuxi Avatar asked Nov 02 '10 23:11

Fuxi


2 Answers

The example code below worked for me. Hope it helps....

 <script type="text/javascript">
function getCell( cell )
{
    var cell || '';
    var result = $('tr').find('td:contains('+cell+')');

    alert( $(result).text() );
}

 <body onload="javascript:getCell('cell 4');">

 <table width="30" border="0" cellspacing="0" cellpadding="0">
<tr>
    <td>cell 1</td>
    <td>cell 2</td>
    <td>cell 3</td>
</tr>
<tr>
    <td>cell 4</td>
    <td>cell 5</td>
    <td>cell 6</td>
</tr>
<tr>
    <td>cell 7</td>
    <td>cell 8</td>
    <td>cell 9</td>
</tr>
 </table>


 </body>
like image 109
exoboy Avatar answered Oct 13 '22 11:10

exoboy


Maybe instead of the second parameter tbl

var td = $("#mySpecificTable td:contains('MyCell')");
like image 28
JustcallmeDrago Avatar answered Oct 13 '22 11:10

JustcallmeDrago