By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,256 Members | 1,700 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,256 IT Pros & Developers. It's quick & easy.

Invoke onclick-Event in JS

P: n/a
Hi,

I create an object like this:
var cell = document.createElement("td").
It doesn't have to be cell.

I want this cell to use the onclick event. What doesn't work in the IE as
well as with Mozilla is:
cell.onclick = "whatever()";

What works only with Mozilla is:
cell.setAttribute("onclick", "whatever()");

So my question: Is there any way to get this done in the IE?

thanks,
Andreas
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
DU
Andreas Knollmann wrote:
Hi,

I create an object like this:
var cell = document.createElement("td").
It doesn't have to be cell.

I want this cell to use the onclick event. What doesn't work in the IE as
well as with Mozilla is:
cell.onclick = "whatever()";

What works only with Mozilla is:
cell.setAttribute("onclick", "whatever()");

It is not recommendable to use setAttribute method when there is a
better way (or a direct way) to set an attribute.
So my question: Is there any way to get this done in the IE?

thanks,
Andreas


RefObjCell.onclick = new Function("evt", "function body here");

e.g.:

RefObjCell.onclick = new Function("evt", "alert('an onclick event just
happened inside a table cell');");

Tested and working in Opera 7.50 PR1 build 3494, MSIE 6 SP1, Mozilla 1.3+

DU
Jul 20 '05 #2

P: n/a
"Andreas Knollmann" <cm**@gmx.net> writes:
I create an object like this:
var cell = document.createElement("td"). I want this cell to use the onclick event. What doesn't work in the IE as
well as with Mozilla is:
cell.onclick = "whatever()";
No, the value assigned to the onclick attribute must be a function,
not a string.
What works only with Mozilla is:
cell.setAttribute("onclick", "whatever()");

So my question: Is there any way to get this done in the IE?

cell.onclick = function(){whatever();}
or even
cell.onclick = whatever;

Notice that most browsers sends the event object as an argument to the
function (as sepcified by W3C DOM Events), while IE keeps it as a
global variable. If your code uses the event object, you will probably
write:
cell.onclick = function(event){event = event||window.event;
// your code using event
};

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.