Bogdan
Bogdan

Reputation: 1901

Why does this onclick event set in Javascript not fire?

The code is simple:

var td1 = document.createElement("td");
td1.id="td_radio_"+nr_int+"_"+nr_var2;
td1.style.border = "0";
td1.style.width = "5%";
td1.onclick="adaugare_varianta_simplu(\'"+nr_int+"\',\'"+nr_var2+"\');";

but the function doesn't fire when I click the cell; what am I doing wrong? I'm not using bind because later on there's gonna be a removeAttr working on it so I want it set up as an attribute.

Upvotes: 0

Views: 186

Answers (2)

GillesC
GillesC

Reputation: 10874

You are assigning a string as event handler so it can not be executed, below is more what you are after I think.

var td1 = document.createElement("td");
td1.id="td_radio_"+nr_int+"_"+nr_var2;
td1.style.border = "0";
td1.style.width = "5%";
td1.onclick = function() {
    adaugare_varianta_simplu(nr_int,nr_var2);
};

Upvotes: 2

Elliot Bonneville
Elliot Bonneville

Reputation: 53311

Think you need this:

td1.onclick="function(){adaugare_varianta_simplu(\'"+nr_int+"\',\'"+nr_var2+"\');}";

You have to wrap the event in a function.

Upvotes: 2

Related Questions