NineCattoRules
NineCattoRules

Reputation: 2428

What's the easiest way to anchor some text to an external link using jQuery?

inside my HTML page I add some text using this:

<script>
    $('.description').text('my description')
</script>

Inside my description I need to place an anchor tag href to an external link like this: <a href="http://link_to_website.com">link to a website</a>

How could I proceed using jQuery to achieve something like this?

Upvotes: 0

Views: 44

Answers (3)

Nishit Maheta
Nishit Maheta

Reputation: 6031

use .html()

   $('.description').html('<a href="http://link_to_website.com">link to a website</a>')

or

$('.description').html('my description <br> <a href="http://link_to_website.com">link to a website</a>')

Upvotes: 1

Jai
Jai

Reputation: 74738

You might need to do this:

$('.description').text('my description').append($('<a />', {href:"http://linkofhref", text:"text goes here."}));

Check the demo below:

$('.description').text('my description ').append($('<a />', {href:"http://linkofhref", text:"text goes here."}));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='description'></div>

Upvotes: 3

Sarath Kumar
Sarath Kumar

Reputation: 2353

try this..

<script>
    $(document).ready(function(){
        $('.description').html('<a href="http://link_to_website.com">link to a website</a>');
});
</script>

Upvotes: 1

Related Questions