how to add text link inside a div using jquery?

user2773294 picture user2773294 · Oct 5, 2013 · Viewed 47.8k times · Source

I want to place some text as link inside a div.

For example, I want to place text 'Google' with hyperlink <a href="http://www.google.com" inside a div having class-id as "my-link".

How can this be done using jquery or javascript?

Answer

Sergio picture Sergio · Oct 5, 2013

Class and ID is not the same.

If ID try this:

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

Demo with ID

If Class try this:

$('.my-link').html('<a href="http://www.google.com">Google</a>');
   ^

Demo with class