I have dynamic HTML structure. Like this:
<div class="message-text">Lorem ipsum http://google.com dolour sit
amet<br> </div>
I want click this http
link and other all links.
How can I find it and make clickable via jQuery?
I have dynamic HTML structure. Like this:
<div class="message-text">Lorem ipsum http://google.com dolour sit
amet<br> </div>
I want click this http
link and other all links.
How can I find it and make clickable via jQuery?
If I understand you right, you could use a normal hyperlink:
<div class="message-text">Lorem ipsum<a href="http://google.com">http://google.com</a> dolour sit amet<br> </div>
If for whatever reason that is not in option, you could try javascript:
<script type="text/javascript">
var a = document.createElement('a');
var linkText = document.createTextNode("Lorem ipsum http://google.com dolour sit
amet<br> ");
a.appendChild(http://google.com);
a.href = "http://google.com";
document.body.appendChild(a);
I hope this helps you at all!
In case you are trying to dynamically replace plain text URLs to clickable URLs (its HTML representation) there is a quite good discussion here
This isn't an exhaustive answer, but it may get you started. You have to take the text, turn it into a url then put it back. Here is how. And here is my fiddle.
$(function(){
var newStr;
var matchStr=$("div.message-text").html();
var REGEX_MATCHER_URL = LINK_DETECTION_REGEX = /(([a-z]+:\/\/)?(([a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|local|internal))(:[0-9]{1,5})?(\/[a-z0-9_\-\.~]+)*(\/([a-z0-9_\-\.]*)(\?[a-z0-9+_\-\.%=&]*)?)?(#[a-zA-Z0-9!$&'()*+.=-_~:@/?]*)?)(\s+|$)/gi
$("div.message-text").each(function(index, div){
var newDiv;
var oldDiv;
var oldTextArray;
var oldDivHtml = $(div).html();
$(div).html(""); // clear it
var matches = oldDivHtml.match(REGEX_MATCHER_URL);
$.each(matches, function(index, value){
var holder = oldDivHtml.split(value);
$(div).append(holder[0]);
$(div).append($("<a>").attr("href", value).html(value));
$(div).append(' ' + holder[1]);
});
});
});