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

add onclick event using setAttribute

P: n/a
All,

I was trying to add an onclick event using setAttribute to a <tdfrom
javascript. Apparently the code doesnot seem to work in FF3 but works
pretty well in IE7.
Here is the snippet for ur reference:
<...
var _table=document.createElement("table");
_tbody=document.createElement("tbody");

var _tr = document.createElement("tr");
var _td = document.createElement("td");
_td.innerHTML="6";
_td.id=""+x+"_"+y+"";
_td.setAttribute("onclick",function(){check(event) ;});
_tr.appendChild(_td);
_tbody.appendChild(_tr);
_table.appendChild(_tbody");
....
>
And here is function "Check's" definition
function check(e){
if (!e)
{
var e=window.event;
alert(e+" :None");
}
if (e.target)
{

targ=e.target;
alert("FF/Netscape: ");
}
else if (e.srcElement)
{
targ=e.srcElement;
alert("IE");
document.getElementById(e.srcElement.id).innerHTML ="0";
}
alert(targ.tagName);
}

Any help in tracking this problem would be really useful.
Jul 19 '08 #1
Share this Question
Share on Google+
14 Replies


P: n/a
On Jul 19, 1:49*pm, Sri02 <sri.ram...@gmail.comwrote:
>
_td.setAttribute("onclick",function(){check(event) ;});
_td.onclick= check;

--Jorge.
Jul 19 '08 #2

P: n/a
Sri02 wrote:
I was trying to add an onclick event using setAttribute to a <tdfrom
javascript. Apparently the code doesnot seem to work in FF3 but works
pretty well in IE7.
setAttribute() has a number of buggy implementations, avoid it.
Here is the snippet for ur reference:
^^
This is not an Internet chat.
<...
var _table=document.createElement("table");
_tbody=document.createElement("tbody");

var _tr = document.createElement("tr");
var _td = document.createElement("td");
_td.innerHTML="6";
Using the proprietary innerHTML property is not necessary for setting
the content of an element to "6". Especially not when you are using
standards-compliant DOM methods already.

_td.appendChild(document.createTextNode("6"));
_td.id=""+x+"_"+y+"";
The leading and trailing concatenation is inefficient and superfluous.

_td.id = x + "_" + y;
_td.setAttribute("onclick",function(){check(event) ;});
As Jorge said.
_tr.appendChild(_td);
_tbody.appendChild(_tr);
_table.appendChild(_tbody");
There is an extra <"character.
...
All of this should be secured by a runtime feature test before use, of course:

if (_td && isMethod(_td, "appendChild"))
{
// ...
}
And here is function "Check's" definition
function check(e){
if (!e)
{
var e=window.event;
alert(e+" :None");
}
if (e.target)
This should be at least

if (e)
{
var t = e.target || e.srcElement;
if (t)
{
// ...
}
}

No need to have a separate branch for each DOM.
HTH

PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Jul 19 '08 #3

P: n/a
Thomas 'PointedEars' Lahn wrote:
Sri02 wrote:
>_td.setAttribute("onclick",function(){check(event );});

As Jorge said.
JFTR, the standards-compliant approach is:

_td.addEventListener("click", function(e) { check(e); }, false);

or

_td.addEventListener(
"click", {handleEvent: function(e) { check(e); }}, false);

But MSHTML does not support it and attachEvent() is not a viable alternative.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Jul 19 '08 #4

P: n/a
"Thomas 'PointedEars' Lahn" <Po*********@web.dewrote in message
news:48**************@PointedEars.de...
Sri02 wrote:
All of this should be secured by a runtime feature test before use, of
course:

if (_td && isMethod(_td, "appendChild"))
{
// ...
}
Thomas,

isMethod ? Where does "isMethod()" come from ? Surely this is not penned by
you ?

Sri02,
_td.setAttribute("onclick",function(){check(event) ;});
As for setting event handlers for both standards compliant and legacy
browser support see my "addEvent - The late entry :)" thread :-

http://groups.google.com/group/comp....45c0d89caf0a1#

Aaron

Jul 20 '08 #5

P: n/a
Aaron Gray wrote:
"Thomas 'PointedEars' Lahn" wrote:
>Sri02 wrote:
All of this should be secured by a runtime feature test before use, of
course:

if (_td && isMethod(_td, "appendChild"))
{
// ...
}

Thomas,

isMethod ? Where does "isMethod()" come from ? Surely this is not penned by
you ?
You may use any `isMethod' implementation you like as long as its license
allows it, including mine.
>_td.setAttribute("onclick",function(){check(event );});

As for setting event handlers for both standards compliant and legacy
browser support see my "addEvent - The late entry :)" thread :-

http://groups.google.com/group/comp....45c0d89caf0a1#
The approach you present there is old news and flawed, as is attachEvent().
I was going to reply to that in more detail later, although this has been
discussed to death here and elsewhere already.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Jul 20 '08 #6

P: n/a
"Thomas 'PointedEars' Lahn" <Po*********@web.dewrote in message
news:48**************@PointedEars.de...
Aaron Gray wrote:
>"Thomas 'PointedEars' Lahn" wrote:
>>Sri02 wrote:
All of this should be secured by a runtime feature test before use, of
course:

if (_td && isMethod(_td, "appendChild"))
{
// ...
}

Thomas,

isMethod ? Where does "isMethod()" come from ? Surely this is not penned
by
you ?

You may use any `isMethod' implementation you like as long as its license
allows it, including mine.
Okay, you should really mention things like that though.
>>_td.setAttribute("onclick",function(){check(even t);});

As for setting event handlers for both standards compliant and legacy
browser support see my "addEvent - The late entry :)" thread :-
http://groups.google.com/group/comp....45c0d89caf0a1#

The approach you present there is old news and flawed, as is
attachEvent().
I was going to reply to that in more detail later, although this has been
discussed to death here and elsewhere already.
Great Thomas, I shall look forward to that then.

Aaron

Jul 20 '08 #7

P: n/a
"Thomas 'PointedEars' Lahn" <Po*********@web.dewrote in message
news:48**************@PointedEars.de...
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Thomas,

You seem to have made it impossible to find the original quoted source.

Could you please provide a proper link from a news archive as demon no
longer store this message and you having quoted it all over the place has
made it impossible to find on Google.

Thanks,

Aaron
Jul 20 '08 #8

P: n/a
On Jul 20, 4:05*am, "Aaron Gray" <ang.use...@gmail.comwrote:
>
Could you please provide a proper link from a news archive as demon no
longer store this message and you having quoted it all over the place has
made it impossible to find on Google.
This message

http://groups.google.com/group/comp....b6cb2b856fc101

points to this thread :

http://groups.google.com/group/comp....1f03af63cc81c2

--Jorge.
Jul 20 '08 #9

P: n/a
Aaron Gray wrote:
"Thomas 'PointedEars' Lahn" <Po*********@web.dewrote:
>Aaron Gray wrote:
>>"Thomas 'PointedEars' Lahn" wrote:
Sri02 wrote:
All of this should be secured by a runtime feature test before use, of
course:

if (_td && isMethod(_td, "appendChild"))
{
// ...
}
Thomas,
Don't.
>>isMethod ? Where does "isMethod()" come from ? Surely this is not penned
by you ?
You may use any `isMethod' implementation you like as long as its license
allows it, including mine.

Okay, you should really mention things like that though.
Subscribers of this newsgroup are supposed to be intelligent beings who are
capable of typing "isMethod" into the text input of their favorite search
engine when they get a runtime error on calling isMethod().
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Jul 20 '08 #10

P: n/a
Aaron Gray wrote:
"Thomas 'PointedEars' Lahn" <Po*********@web.dewrote:
>Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>

Thomas,
Don't.
You seem to have made it impossible to find the original quoted source.
Yes, it only seems to be so.
Could you please provide a proper link from a news archive as demon no
longer store this message and you having quoted it all over the place has
made it impossible to find on Google.
Having yourself evaluate the following as an ECMAScript Program will help:

if (me.googlodyte)
{
with (me.brainSimulation)
{
if (navigator.go(
"http://groups.google.com/advanced_search",
"http://groups.google.com/",
i18n("Advanced Groups Search")))
{
if (navigator.paste(
navigator.copy(perception.currentPosting.body
.getNextEmailAddr()),
i18n("Lookup the message with message ID")))
{
if (navigator.click(i18n("Lookup Message")))
{
while (navigator.readyState != 4);
navigator.read();
}
}
}
}

while (!me.hasMinimumClue())
{
if (me.getInformedAbout("Usenet"))
{
me.getLocalNewsReaderApp();
}
}

me.googlodyte = false;
}
else
{
me.lookupMsgId(me.brain.perception.currentPosting. body.getNextMsgId());
}

Probably someone has told you this before.
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Jul 20 '08 #11

P: n/a
"Jorge" <jo***@jorgechamorro.comwrote in message
news:01**********************************@34g2000h sh.googlegroups.com...
On Jul 20, 4:05 am, "Aaron Gray" <ang.use...@gmail.comwrote:
>
Could you please provide a proper link from a news archive as demon no
longer store this message and you having quoted it all over the place has
made it impossible to find on Google.
This message

http://groups.google.com/group/comp....b6cb2b856fc101

points to this thread :

http://groups.google.com/group/comp....1f03af63cc81c2

Thanks,

Aaron
Jul 20 '08 #12

P: n/a
"Thomas 'PointedEars' Lahn" <Po*********@web.dewrote in message
news:48**************@PointedEars.de...
Aaron Gray wrote:
>"Thomas 'PointedEars' Lahn" <Po*********@web.dewrote:
>>Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>

Thomas,

Don't.
>You seem to have made it impossible to find the original quoted source.

Yes, it only seems to be so.
>Could you please provide a proper link from a news archive as demon no
longer store this message and you having quoted it all over the place has
made it impossible to find on Google.

Having yourself evaluate the following as an ECMAScript Program will help:

if (me.googlodyte)
{
with (me.brainSimulation)
{
if (navigator.go(
"http://groups.google.com/advanced_search",
"http://groups.google.com/",
i18n("Advanced Groups Search")))
{
if (navigator.paste(
navigator.copy(perception.currentPosting.body
.getNextEmailAddr()),
i18n("Lookup the message with message ID")))
{
if (navigator.click(i18n("Lookup Message")))
{
while (navigator.readyState != 4);
navigator.read();
}
}
}
}

while (!me.hasMinimumClue())
{
if (me.getInformedAbout("Usenet"))
{
me.getLocalNewsReaderApp();
}
}

me.googlodyte = false;
}
else
{
me.lookupMsgId(me.brain.perception.currentPosting. body.getNextMsgId());
}

Probably someone has told you this before.
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Have you not been told not to use the 'with' clause ?

Aaron
Jul 20 '08 #13

P: n/a
Aaron Gray wrote:
Have you not been told not to use the 'with' clause ?
Have you not been told to quote properly?
Score adjusted

PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Jul 20 '08 #14

P: n/a
On Jul 20, 10:24*am, "Aaron Gray" <ang.use...@gmail.comwrote:
"Thomas 'PointedEars' Lahn" <PointedE...@web.dewrote in messagenews:48**************@PointedEars.de...
Sri02 wrote:
All of this should be secured by a runtime feature test before use, of
course:
*if (_td && isMethod(_td, "appendChild"))
*{
* *// ...
*}

Thomas,

isMethod ? Where does "isMethod()" come from ? Surely this is not penned by
you ?
He posted a version in this thread:

<URL:
http://groups.google.com.au/group/co...2963d7ec6dbc8b
>

A modified version of that is:

var isMethod = (function() {
var re = /\bfunction|object\b/;
return function(o) {
return re.test(typeof o);
}
})();
to me that is only marginally more effective than:

if (obj) obj();

or

obj && obj();
which at least saves looking up the isMethod function to see what it
does.

The definition of a function in javscript is an object that implements
the internal [[Call]] method[1], however there is no definitive test
for that, the closest is to actually call it within a try..catch block
and see what eventuates, though that can also be error-prone and
likely far more effort than it's worth in the vast majority of cases.

Using typeof should be sufficient for native functions, however host
functions do not have to return 'function' and don't in at least one
popular browser.

You might like this thread if you have an hour or so spare - there's a
lot to read! :-)

<URL:
http://groups.google.com.au/group/co...7385810dfcdf9a
>

1. That isn't explicitly stated in ECMA-262 as far as I know, but can
be reasonably inferrred from sections 10.1.3 and 11.4.3
--
Rob
Jul 21 '08 #15

This discussion thread is closed

Replies have been disabled for this discussion.