Andrew Jackson
Andrew Jackson

Reputation: 11

Add link to text - with the text in the link

This is to tricky for me,..

..here we go. I have a <table> like this:

<table>
<tr>
<td class="cell0">01720007663795101</td>
</tr>
</table>

Now i would like to create a link arround the Text in the <td> like this:

<a href="https://tracking.dpd.de/cgi-bin/delistrack?pknr=01720007663795101&typ=1&lang=de">01720007663795101</a>

so you can see there are 4 steps to do.

  1. grab the number in the <td> by classname
  2. create a link arround the number, beginning with <a href="https://tracking.dpd.de/cgi-bin/delistrack?pknr=
  3. append the number from <td> after <a href="https://tracking.dpd.de/cgi-bin/delistrack?pknr=
  4. append the rest of the link &typ=1&lang=de"> after <a href="https://tracking.dpd.de/cgi-bin/delistrack?pknr=01720007663795101

Upvotes: 1

Views: 1922

Answers (2)

Frank van Wijk
Frank van Wijk

Reputation: 3262

var className = 'cell0';
td = $('td.' + className);
var link = '<a href="https://tracking.dpd.de/cgi-bin/delistrack?pknr=' + td.text() + '&typ=1&lang=de">' + td.text() + '</a>';
td.html(link);

See JsFiddle -> update JsFiddle

Upvotes: 2

Alex
Alex

Reputation: 6406

http://jsfiddle.net/YuK6y/

$('.cell0').each(function(index, element){
    var tn = $(element).text();
    $(element).html('<a href="https://tracking.dpd.de/cgi-bin/delistrack?pknr='+tn+'&typ=1&lang=de">'+tn+'</a>');
});

Or, shorter: http://jsfiddle.net/YuK6y/1/

$('.cell0').each(function(index, element){
    $(element).wrapInner('<a href="https://tracking.dpd.de/cgi-bin/delistrack?pknr='+$(element).text()+'&typ=1&lang=de" />');
});

Upvotes: 3

Related Questions