How Do I Create a Link Using Javascript?
I Have a String for a Title and a String for a Link. I'm Not Sure How to Put the Two Together to Create a Link on a Page Using Javascript. Any Help Is...
I have a string for a title and a string for a link. I'm not sure how to put the two together to create a link on a page using Javascript. Any help is appreciated.
EDIT1: Adding more detail to the question. The reason I'm trying to figure this out is because I have an RSS feed and have a list of titles ands URLs. I would like to link the titles to the URL to make the page useful.
EDIT2: I am using jQuery but am completely new to it and wasn't aware it could help in this situation.
8 Answers
<html>
<head></head>
<body>
<script>
var a = document.createElement('a');
var linkText = document.createTextNode("my title text");
a.appendChild(linkText);
a.title = "my title text";
a.href = "";
document.body.appendChild(a);
</script>
</body>
</html>
Must Read
With JavaScript
var a = document.createElement('a'); a.setAttribute('href',desiredLink); a.innerHTML = desiredText; // apend the anchor to the body // of course you can append it almost to any other dom element document.getElementsByTagName('body')[0].appendChild(a);document.getElementsByTagName('body')[0].innerHTML += '<a href="'+desiredLink+'">'+desiredText+'</a>';or, as suggested by @travis :
document.getElementsByTagName('body')[0].innerHTML += desiredText.link(desiredLink);<script type="text/javascript"> //note that this case can be used only inside the "body" element document.write('<a href="'+desiredLink+'">'+desiredText+'</a>'); </script>