user3541562
user3541562

Reputation: 271

Defining function call dynamically with javascript to anchor tag

I have an anchor tag with id=1 and I am trying to define onClick="menu_function(shopname)" using the logic below, but it's not working like a.href:

function display_details(shopname,img)
{
    var a = document.getElementById("1");
    a.href = "./menu.php";
    a.onClick ="menu_function(shopname)";
}

Can you tell my why is it not working and guide me into the right direction?

Upvotes: 1

Views: 301

Answers (1)

MrCode
MrCode

Reputation: 64526

Use a function expression as the value for onclick, not a string:

 function display_details(shopname,img)
 {
     var a = document.getElementById("1");
     a.href = "./menu.php";
     a.onclick = function(){
        menu_function(shopname);
     };
 }

Also note: JavaScript is case sensitive and the event is called onclick (lowercase) not onClick.

Upvotes: 3

Related Questions