Aadil Afzal
Aadil Afzal

Reputation: 63

Anchor tag is not working in javascript

I use document.write() to print the anchor tag, but it not working here is my code, so can any one help me out where i am wrong. Thanks

var name = 'addtowish<?php echo $row->fields("id");?>';
var value = '<?php echo $row->fields("id");?>';    
document.write(
    "<a href='#' onclick='"+ createCookie(name, value, '15') +"'>" +
        "add to wishlist" +
    "</a>"
);

Upvotes: 0

Views: 229

Answers (2)

Matthew Riches
Matthew Riches

Reputation: 2286

If you are after a robust and pure JS solution, something like this should do the trick, but it depends on what your end game is:

var anchor      = document.createElement("a");
var anchorText  = document.createTextNode("add to wishlist");
anchor.appendChild(anchorText);
anchor.href     = "#";
anchor.id       = "addLink";

document.write(anchor);

window.onload = function() {
    var name = "addtowish";
    var value = 99;
    document.getElementById("addLink").onclick = function(e) {
        createCookie(name, value, 15);    
        e.preventDefault();
        return false;
    };
};

Upvotes: 0

Satpal
Satpal

Reputation: 133453

You should pass createCookie() function as string

document.write(
    "<a href='#' onclick=\"createCookie('"+ name +"','" + value +"', '15')\">" +
        "add to wishlist" +
    "</a>"
);

Upvotes: 2

Related Questions