How do I create a link using javascript?

Xavier picture Xavier · Jan 23, 2011 · Viewed 397.5k times · Source

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.

Answer

Jared Farrish picture Jared Farrish · Jan 23, 2011
<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 = "http://example.com";
      document.body.appendChild(a);
    </script>
  </body>
</html>