0

How can I add an hyperlink to a <td> in a dynamic table?

I need the 1st <td> to be an hyperlink to a url + cell value.

Table dynamic creation :

for (var i = 0; i < riskData.length; i++) {
  $("#grdDemo3").append("<tr><td>" + riskData[i].r_id +
    "</td><td>" + riskData[i].r_team + "</td></tr>");
}
3
  • What did you try? How does a hyperlink look like? Commented Feb 19, 2020 at 12:38
  • And the problem is? Just add the markup for the link like you've done with the table row and the cells. Commented Feb 19, 2020 at 12:38
  • You should also HTML-encode the team name in case someone puts <script> tags or similar in their team name. Some approaches to do that here: stackoverflow.com/a/7124052/243245, or you could build the entities separately in jQuery and use .text() to set the team name etc. Commented Feb 19, 2020 at 12:42

2 Answers 2

1

Try this

for (var i = 0; i < riskData.length; i++) {
   $("#grdDemo3").append("<tr><td><a href='https://a.com/"+riskData[i].r_id +"'>" +riskData[i].r_id + "</a></td><td>" + riskData[i].r_team + "</td></tr>");

 }

OR

for (var i = 0; i < riskData.length; i++) {
   $("#grdDemo3").append("<tr><td onclick='window.location.href=\"htts://a.com/"+riskData[i].r_id +"\"'>" +riskData[i].r_id + "</td><td>" + riskData[i].r_team + "</td></tr>");

 }
Sign up to request clarification or add additional context in comments.

Comments

0

You can always add an anchor tag to td element and make it look like a whole table cell.

table {
  border-collapse: collapse;
}

td {
  border: 1px solid #CCCCCC;
  padding: 12px;
}

table tr td a {
  display: block;
  height: 100%;
  width: 100%;
  background: #F2F2F2;
  text-decoration: none;
}
<table>
  <tr>
    <td>
      <a href="https://www.google.com/"><span>Some Text</span></a>
    </td>
    <td>some content
    </td>
  </tr>
  <tr>
    <td>
      hello here </td>
    <td>some content
    </td>
  </tr>
  <tr>
    <td>
      <a href="https://www.google.com/"><span>Some Text</span></a>
    </td>
    <td>some content
    </td>
  </tr>
  <tr>
    <td>
      <a href="https://www.google.com/"><span>Some Text</span></a>
    </td>
    <td>some content
    </td>
  </tr>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.