Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add text link inside a div using jquery?

Tags:

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?

like image 815
user2773294 Avatar asked Oct 05 '13 14:10

user2773294


People also ask

How do you add a link to a div?

Create HTML Create a <div> with a class name "container". Use the <a> element to add the needed link.

What does .text in jQuery do?

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.


2 Answers

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

like image 118
Sergio Avatar answered Sep 20 '22 12:09

Sergio


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.

like image 34
Subin Avatar answered Sep 20 '22 12:09

Subin