April 22, 2013
How to replace text with links using javascript?
Hick’s Questions:
This is what I do to take out texts from a html usng Jquery:
$(".text").each(function(){
var texts = items[textCount]['data']['title'];
$(this).text(texts);
textCount = textCount + 1;
});
My problem if I want the texts to be a url, then it doesn’t show up as link but also a string added to the texts variable. How do I show a link? How can add it as a href to the texts?
Output should be something like this:
Text + url
and not text being linked as url: "<a href=""+link+"">"+texts+"</a>"
Using .attr()
function like this.
$(this).attr("href", "your link");
But this will only work, if you have an anchor tag if not you can create an anchor tag on the fly.
$(this).html("<a href=""+link+"">"+texts+"</a>");