wow
wow

Reputation: 8239

It's possible to replace existing html code with jQuery

Example current html code

<a rel="tag" title="see questions tagged 'php'" href="/tags/php/" class="tag-link-php">php</a>

I want use jQuery to replace it to be

<a href="http://php.net/"><img src="/images/tags/php.gif" height="16" width="16" alt="php" /></a><a rel="tag" title="see questions tagged 'php'" href="/tags/php/" class="tag-link-php">php</a>

Let me know

Upvotes: 0

Views: 307

Answers (4)

jAndy
jAndy

Reputation: 235992

Looks like:

$("<a/>", { 
   "href":  "http://www.php.net"
}).append($("<img>", {
   "src":    "/images/tags/php.gif",
   "height": "16",
   "width":  "16",
   "alt":    "/images/tags/php.gif"
})).insertBefore(".tag-link-php");

Upvotes: 2

Vojta
Vojta

Reputation: 23051

Replace whole element / elements:

http://api.jquery.com/replaceWith/

Or just change some attributes:

http://api.jquery.com/attr/

Upvotes: 1

codersarepeople
codersarepeople

Reputation: 2001

You can also use replaceWith:

http://api.jquery.com/replaceWith/

Upvotes: 1

lonesomeday
lonesomeday

Reputation: 237855

So it seems to me that you want to put the following code

<a href="http://php.net/"><img src="/images/tags/php.gif" height="16" width="16" alt="php" /></a>

before any links with the class tag-link-php. You can use the before function for this:

$('a.tag-link-php').before('<a href="http://php.net/"><img src="/images/tags/php.gif" height="16" width="16" alt="php" /></a>');

Upvotes: 3

Related Questions