1

Does anyone have any idea how I can do this? I want my content writer to be able to add span classes to certain numerical amounts that then automatically hyperlink to a pre-defined URL. Example:

And the price at John's hardware shop are<span class="john-shop">$2.35</span> 

Can this be done with php or even css?

Grateful for any help.

desired outcome:

And the price at John's hardware shop are <a href="http://www.johns-shop.com"><span class="john-shop"> $2.35</span></a>

Ok, after much trial and error, this code works. Hope it helps someone else. The "link.js" contains this code:

$( document ).ready(function() {
    $('.john-shop').contents().wrap('<a href="http://www.johns-shop.com"></a>');
  });

and this is the test html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en">
<head>
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="js/link.js"></script>
<title></title>
</head>
<body>
And the price at John's hardware shop are<span class="john-shop">$2.35</span>
</body>
</html>
That1Guy
  • 7,075
  • 4
  • 47
  • 59
Rich Stevens
  • 599
  • 4
  • 17

1 Answers1

3

Since you specified in your comment

I can use whatever does the job.

So here is the jQuery solution for that:

$('.john-shop').contents().wrap('<a href="http://www.johns-shop.com"></a>');

Fiddle Here

P.S. It won't be possible to achieve this using html/css simply, as far as I know. You must use javascript in order to achieve that.

UPDATE You may find a pure javascript related solution here at make hyperlink from javascript

Community
  • 1
  • 1
Kamran Ahmed
  • 11,809
  • 23
  • 69
  • 101