user1348226 user1348226 - 11 days ago 6
HTML Question

How to put link to the table cell using JQuery

Below there is simple/example table.
For the question is on how can I put a link for the particular table cell that I need.
For example, when i click on the first cell of the table "row 1, cell 1", so it will execute the link and jump to the next site.

<table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>


Thanks for for SHARING.

Answer

You could give your specified table cell an id (such as "linker") and then add a click event handler

jQuery:

$("td#linker").click(function(e)
{
    // Make your content bold
    $(this).css("font-weight","bold");
    // Direct the page like a link
    window.location.href="<WHER YOU WANT IT TO GO>";
    // Prevent the click from going up the chain
    e.stopPropagation();
});

HTML:

<table border="1">
    <tr>
        <td id="linker">Click Me</td>
        <td>row 1, cell 2</td>
    </tr>
    <tr>
        <td>row 2, cell 1</td>
        <td>row 2, cell 2</td>
    </tr>
</table>