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?
Create HTML Create a <div> with a class name "container". Use the <a> element to add the needed link.
jQuery text() Method The text() method sets or returns the text content of the selected elements. When this method is used to return content, it returns the text content of all matched elements (HTML markup will be removed). When this method is used to set content, it overwrites the content of ALL matched elements.
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
You can do this :
$('.my-link').html('<a href="http://www.google.com">Google</a>');
but it would add hyperlink to all .my-link divs, so it's better to add an ID to div and use the ID on jQuery code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With