kodai
kodai

Reputation: 15

adding href using javascript append

Probably a rudimentary for some, but this is driving me crazy! I am not sure what I did wrong - I cannot/not allowed(?) to append a href - it is simply didn't get processed at all.

$('#lastViewed').append('<a href="/Path/To?_q="' + string + '>');
$('#lastViewed').append(.....some other stuffs.....);
$('#lastViewed').append('</a>');

I am trying to wrap the "other stuffs" with a Thanks!

Edit
The complete line:

$('#lastViewed').append('<div id="id_' + x + '<a href="/PVProduct/ProductDetail?_productID=' + JSON.parse(localStorage.getItem("pid_" + x)).productID);

Upvotes: 0

Views: 140

Answers (2)

Bishoy Bisahi
Bishoy Bisahi

Reputation: 377

you can use it

var link=document.createElement("a");
link.id="idName";
link.setAttribute("href", "your link");
document.appendChild(link);

Upvotes: 0

An Phan
An Phan

Reputation: 2568

This is incorrect. You don't append() opening and closing tags like that. Instead, append() the whole tag. Or even better, create the a element and append() it:

$a = $('<a>').attr('href', yourHref).html(yourText);
$('#lastViewed').append($a);

Upvotes: 1

Related Questions