0

I have a <div> element that when clicked updates content on a page. How do I go about adding a link with this JS code? I made a JSFiddle of basically my question. Basically the "" are ending too soon so there must be a character like %20 to do my dirty work. Is it possible?

http://jsfiddle.net/BpCSm/

$("#content").html("<a herf="http://www.google.com">google.com</a>");
ajp15243
  • 7,704
  • 1
  • 32
  • 38
Michael Schmidt
  • 396
  • 1
  • 3
  • 15
  • 1
    The following SO ticket has the answer for you. http://stackoverflow.com/questions/4772774/how-do-i-create-a-link-using-javascript – Alec. May 28 '13 at 22:10
  • 2
    Please look at the console when running your code, you will see you have syntax errors.. – dsgriffin May 28 '13 at 22:13

2 Answers2

4

Use single quotes inside the double quotes:

$("#content").html("<a href='http://www.google.com'>google.com</a>"); } ); });

Also, it's not herf but href

This is the adapted Fiddle: http://jsfiddle.net/Kennethtruyers/BpCSm/4/

Kenneth
  • 28,294
  • 6
  • 61
  • 84
1

There are 2 ways to handle this

Using combination of singlequote and doublequote like this "'" or '"'

$("#content").html("<a href='http://www.google.com'>google.com</a>");

or , using the escape character like this "\"" or '\''

$("#content").html("<a href=\"http://www.google.com\">google.com</a>");
Rohit Agrawal
  • 5,372
  • 5
  • 19
  • 30