N1G3L
N1G3L

Reputation: 267

JavaScript variable as HTML link text

I am trying to something like this:

<a href="www.link.com">JS variable</a>

I have seen multiple threads where someone is trying to do something similar but none of these solutions seem to be working for me. I am trying to pull data from an API and I would like to use a variable from the API as the text for the link. Right now I am using jquery to do this:

$('#tracktitle').append($("<a href='" + track.permalink_url + "' target='_blank'>Title</a><br>").html(track.title));

but would like to have the 'html(track.title)' in place of 'Title'

any help would be greatly appreciated

Upvotes: 0

Views: 1835

Answers (2)

nnnnnn
nnnnnn

Reputation: 150070

When you create a new element with jQuery you can pass just the tag as the first argument to $() and then pass an object with the various properties you want to set as the second argument:

$("<a></a>", {
    href : track.permalink_url,
    html : track.title
}).appendTo("#tracktitle")
.after("<br>");

Demo: http://jsfiddle.net/7CVnZ/

Upvotes: 3

Leo
Leo

Reputation: 580

What about? $('#tracktitle').append("<a href='" + track.permalink_url + "' target='_blank'>" + track.title +"</a><br>")

Upvotes: 3

Related Questions