0

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);
2
  • 2
    What's string? Also, you're closing the " too early. $('#lastViewed').append('<a href="/Path/To?_q=' + string + '">'); will probably work - can I close as typo? Also, cache your selectors. Commented Aug 17, 2014 at 8:00
  • 1
    '<div id="id_' + x + '<a: You don't have a closing > for the div. Commented Aug 17, 2014 at 8:05

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for pointing me to the correct way of using the append keyword. I ended up building a string and insert it in the <a> tag. Thanks much!
Glad I could help. Remember, jQuery had appendTo() as well.
0

you can use it

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.