Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select <td> of the <table> with javascript?

Tags:

People also ask

How do I get the TD value?

jQuery: Code to get table cell value of specific HTML element (div or span) content using jquery. As we need to access specific div/span content inside table TD, we will use jquery . find() method. Using the jQuery .

How do I get TD in HTML?

The <td> tag defines a standard data cell in an HTML table. An HTML table has two kinds of cells: Header cells - contains header information (created with the <th> element) Data cells - contains data (created with the <td> element)

What is TD and TR in Javascript?

The <td> tag defines the standard cells in the table which are displayed as normal-weight, left-aligned text. The <tr> tag defines the table rows. There must be at least one row in the table. The <th> tag defines the header cells in the table which are displayed as bold, center-aligned text.

How can get TD value from TR in jquery?

var Something = $(this). closest('tr'). find('td:eq(1)'). text();


I know this is very easy question, but I couldn't find the answer anywhere. Only answers are the ones using jQuery, not pure JS. I've tried the code below and it doesn't work. I don't know why.

var t = document.getElementById("table"),
    d = t.getElementsByTagName("tr"),
    r = d.getElementsByTagName("td");

This also doesn't work:

var t = document.getElementById("table"),
    d = t.getElementsByTagName("tr"),
    r = d.childNodes;

What am I doing wrong? What is the best way to do this?

EDIT: I indeed have the id of my table table. Preety silly I know. This is how my HTML looks:

<table id="table">
            <tr>
                <td id="c1">1</td>
                <td id="c2">2</td>
                <td id="c3">3</td>
            </tr>
            <tr>
                <td id="b1">4</td>
                <td id="b2">5</td>
                <td id="b3">6</td>
            </tr>
            <tr>
                <td id="a1">7</td>
                <td id="a2">8</td>
                <td id="a3">9</td>
            </tr>
</table>

To explain my intentions more clearly > I wish to make a tic tac toe game. For starters, I wish to click on the < td > and be able extract the id of that particular < td >. How to do it most efficiently?