ErrorMaster
ErrorMaster

Reputation: 150

Creating a hyperlink in javascript

I am trying to create a hyperlink to a facebook webpage within a message bar on my website. Here is my code

        var str = "Check us out on facebook";
        var result = str.link("https://www.facebook.com/pages/");

         $( "#message-bar" ).click(
        function() {
          $("#message-form").toggle();
          if($("#message" ).text() == "Thanks for stopping by mysite.ca!"){
            $("#message" ).text(result)
          }else{
             $("#message-bar").hide();
          }
        });

Currently after you click the message bar once, instead of displaying the hyperlink it displays the HTML code https://www.facebook.com/pages/"> Check us out on facebook! . So my question is what should I fix to make the actual link appear instead of the code. I've been playing around/ researching the .innerHTML tag although cannot find a working solution. Any input would be apprciated, thanks.

Upvotes: 0

Views: 92

Answers (2)

mildog8
mildog8

Reputation: 2154

Try this

    var str = "Check us out on facebook";
    var result = str.link("https://www.facebook.com/pages/");

     $( "#message-bar" ).click(
    function() {
      $("#message-form").toggle();
      if($("#message" ).text() == "Thanks for stopping by mysite.ca!"){
        $("#message" ).html(result)
      }else{
         $("#message-bar").hide();
      }
    });

Upvotes: 2

SLaks
SLaks

Reputation: 888107

If you want to put HTML in an element, you should call html(), not text().

Upvotes: 2

Related Questions