Search code examples
javascriptjqueryhtmlappendappendto

Make word in into a link jQuery


I'm trying to make this:

<td class="Monthly Status-cell">/fileid=XXXX</td>

Display as

http://www.domain.com/fileid=XXXX

Can you tell me what is wrong with my code?

$('.Status-cell').replaceWith(function() {
var url = $.trim($(this).text());
return '<a href="' + url + '" target="_blank">' + url + '</a>';
});

Thanks!


Solution

  • Use .html() instead of .replaceWith(). While using replaceWith you are replacing the td with anchor inside your table, which is invalid and that must be causing your alignment to get messed up.

    $('.Status-cell').html(function(_, currentText) {
       var url = "http://www.domain.com" + $.trim(currentText);
       return '<a href="' + url + '" target="_blank">' + url + '</a>';
    });
    

    Fiddle