My problem is this:
I am trying to create some cells in a table, which will be filled by data that I extract from an XML document, but I also want those cells to be hyperlinks. I am doing this:
var table = $('<table></table>').addClass('table');
for(i=0;i<x.length;i++){
var row = $('<tr></tr>').text(x.item(i).getAttribute("id")).attr('href', 'www.my-url.com/'+x.item(i).getAttribute("id"));
table.append(row);
}
The cells are filled correctly with the text that I want, but the cells are not clickable. I do not get any errors in the console. Any idea what I am doing wrong?
You can’t set an href attribute on a
<tr>tag and expect it to work like an<a>tag. Instead, you need to put an<a>tag inside the<tr>tag. You can do that like this:That method creates the HTML and lets the browser parse it. You could also create the elements directly like this: