Could someone explain in the most simplistic terms possible why this doesn't work:

var cell00501=row005.insertCell(-1);
  cell00501.align="center";
  cell00501.width="8%";
  cell00501.onclick="RadioClicked(0)";
  cell00501.innerHTML="<IMG SRC='RadioOn.jpg' BORDER='0' NAME='Spangle0'>";

By doesn't work I mean the onclick event doesn't result in the RadioClicked() function being called. I have come up with the following workaround:

var cell00503=row005.insertCell(-1);
  cell00503.align="center";
  cell00503.width="8%";
  cell00503.innerHTML="<DIV onclick='RadioClicked(1)'><IMG SRC='RadioOff.jpg' BORDER='0' NAME='Spangle1'></DIV>"

However I would like to understand where I am going wrong.

Many thanks.

Recommended Answers

All 2 Replies

Hi,
I think these examles explain you way "onclick" not works in your script.
This works:

element.onclick = function_name;

Not works:

element.onclick = function_name(argument);

Not works:

element.onclick = "function_name(argument)";

Works:

element.onclick = function() { function_name(argument); };

Many thanks for your reply.

So the below is the correct syntax for calling a function when passing a value/argument into it:

element.onclick = function() { function_name(argument); };

but it doesn't seem a very consice approach. If

element.onclick = function_name;

works for calling a function that doesn't require anything to be passed into it why doesn't:

element.onclick = "function_name(argument)";

work when the function does require a value/argument to be passed into it?

It all seems very illogical.

Just out of interest would

cell00503.innerHTML="<DIV onclick='RadioClicked(1)'><IMG SRC='RadioOff.jpg' BORDER='0' NAME='Spangle1'></DIV>"

cause problems with some browsers? Would it be considered an acceptable piece of code?

Many thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.