0

I have dynamically generated text that may contain links to webpages. I want to automatically detect if a user has "www.someaddress.com" or "http://someaddress.com" or "http://www.someaddress.com" and make it a true hyperlink instead of just text.

HERE IS THE HTML:

Hello my name is Jake and I made a website. Check it out at www.jakewebsite.com.
<div class="text_container">
Hello my name is Frank and I made a website. Check it out at www.frankwebsite.com. 

</div>
<div class="text_container">
Hello my name is Jeff and I made a website. Check it out at www.jeffwebsite.com. 

</div>

I attempted to try and write some jQuery code to at the least wrap the www expression in aHref tags but all its doing is putting the aHref at the begingin of the text_container. I do not know how to wrap the ahref around the entire text to make it a hyperlink either.

var foundin = $('*:contains("www.")');

foundin.prepend("<a href='test.php'>");
foundin.append("</a>");
Die 20
  • 261
  • 2
  • 6
  • 16
  • 3
    You will need a lot more logic to do this. I recommend a regular expression. Remember, websites can start with things other than "www" and end with things other than ".com". – deweyredman Nov 07 '13 at 17:53
  • 2
    [This](http://stackoverflow.com/questions/1500260/detect-urls-in-text-with-javascript) might help you – kei Nov 07 '13 at 18:08

1 Answers1

0

You should probably be using regular expressions to recognize the link as deweyredman said. Also, you are seeing the current behavior because of the way that :contains(), prepend() and append() works. What you are doing here is looking for all instances where a tag contains the string "www.", then prepending the tag to the inner html and then appending the tag to that. Thus getting your behavior. Instead you what you want to do is reassign the html like so.

var foundin = $('div:contains("www.")');

var textVal = foundin.text();
foundin.html("<a href='test.php'>" + textVal + "</a>");
Brandon Kindred
  • 1,468
  • 14
  • 21