pyy
pyy

Reputation: 953

Getting Ajax data into a table, and building a hyperlink

At the moment, I am reading data from an ajax call and appending it to a table on my page. This works fine, however I would like to also take one of the pieces of data and turn it into a hyperlink.

This is my current code:

$.each(opts, function(i, d) {
$('<tr>').append(
    $('<td>').text(d.PropNo),
    $('<td>').text(d.StoreName),
    $('<td>').text(d.StoreAddress),
    $('<td>').text(d.ClientName),
    $('<td>').text(d.StoreTypeName)
).appendTo('#StoresTable');

I've tried building the hyperlink by adding HTML elements to the append statement like this:

$('<td>').text("<a href=viewstore.php?StoreID="+d.StoreID+">click here to view the store</a>")

But this just results in the HTML elements being treated as text rather than elements.

Does anyone have any suggestions?

Upvotes: 0

Views: 543

Answers (1)

user669677
user669677

Reputation:

I show you another way:

$('input').click(function(){
  $('table').append('\
    <tr>\
      <td><a href="#">dfs</a></td>\
    </tr>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr><th>title</th></tr>
</table>
<input type=button value=add>

Upvotes: 3

Related Questions