HighDots Forums  

The mothod "on" not available for a button?!

Javascript JavaScript language (comp.lang.javascript)


Discuss The mothod "on" not available for a button?! in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
K Viltersten
 
Posts: n/a

Default The mothod "on" not available for a button?! - 05-23-2008 , 05:08 AM






It's perhaps something elementary. I tried
to do suff as follows.

document.getElementById ('Button1').on (
'click', funcion () {});

For some reason, the method isn't there. According
to FireBug i get this.

document.getElementById ('Button1').on
(gives nothing - no such method)

while

Ext.get ('Button1').on
(gives function ())

What do i miss?

--
Regards
Konrad Viltersten

Reply With Quote
  #2  
Old   
Robin Rattay
 
Posts: n/a

Default Re: The mothod "on" not available for a button?! - 05-23-2008 , 05:20 AM






On 23 Mai, 12:08, "K Viltersten" <t... (AT) viltersten (DOT) com> wrote:
Quote:
It's perhaps something elementary. I tried
to do suff as follows.

document.getElementById ('Button1').on (
'click', funcion () {});

For some reason, the method isn't there. According
to FireBug i get this.
Because there isn't such a method.

Quote:
document.getElementById ('Button1').on
(gives nothing - no such method)
Right. document.getElementById returns a DOM reference, and the DOM
API doesn't have a method "on".

Quote:
Ext.get ('Button1').on
(gives function ())
Ext.get is a method of a 3rd party libary (I'm guessing "Ext" :-) that
returns something else than a DOM reference which does have an "on"
method.

I'd suggest as a JavaScript beginner, you should avoid using 3rd party
libraries until you know what you're doing, or at least read their
documentation.

With the DOM API events are set either directly:

document.getElementById('Button1').onclick = function() { ... }

or with addEventListener:

document.getElementById('Button1').addEventListene r("click",
function() { ... }, true);

Which IE however doesn't support. You need to use attachEvent for IE.
More details at: http://developer.mozilla.org/en/docs...dEventListener

Robin


Reply With Quote
  #3  
Old   
Henry
 
Posts: n/a

Default Re: The mothod "on" not available for a button?! - 05-23-2008 , 05:27 AM



On May 23, 11:08 am, K Viltersten wrote:
Quote:
It's perhaps something elementary. I tried
to do suff as follows.

document.getElementById ('Button1').on (
'click', funcion () {});

For some reason, the method isn't there. According
to FireBug i get this.

document.getElementById ('Button1').on
(gives nothing - no such method)

while

Ext.get ('Button1').on
(gives function ())

What do i miss?
That - document.getElementById - returns an element from the DOM and -
Ext.get -(whatever that is) either returns a different sort of object
or returns a DOM element that has been subject to (direct or indirect)
augmentation prior to being returned.

Neither the W3C DOM specifications nor historical practice suggest
that an element should be expected to have an - on - method.


Reply With Quote
  #4  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: The mothod "on" not available for a button?! - 05-23-2008 , 05:42 AM



Robin Rattay wrote:
Quote:
With the DOM API events are set either directly:

document.getElementById('Button1').onclick = function() { ... }

or with addEventListener:

document.getElementById('Button1').addEventListene r("click",
function() { ... }, true);
Reference Worms[tm] are error-prone, and should therefore be avoided.

// add feature tests here

var o = document.getElementById('Button1');
if (o)
{
// add feature tests here

o.addEventListener("click", function() { ... }, true)
}

Adding a capturing event listener (`true') through the standards-compliant
addEventListener() method of the EventTarget interface is _not_ equivalent
to assigning to the proprietary event handler property (`on...').
Therefore, the third argument should be `false' unless compatibility with
non-DOM2 UAs is not an issue (seldom).

Quote:
Which IE however doesn't support. You need to use attachEvent for IE.
However, IE/MSHTML does support the proprietary event handler property,
which should to be used instead of attachEvent():

http://www.quirksmode.org/blog/archi...nt_consid.html


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


Reply With Quote
  #5  
Old   
K Viltersten
 
Posts: n/a

Default Re: The mothod "on" not available for a button?! - 05-23-2008 , 05:46 AM



Quote:
It's perhaps something elementary. I tried
to do suff as follows.

document.getElementById ('Button1').on (
'click', funcion () {});

For some reason, the method isn't there. According
to FireBug i get this.

Because there isn't such a method.

document.getElementById ('Button1').on
(gives nothing - no such method)

Right. document.getElementById returns a DOM reference, and the DOM
API doesn't have a method "on".

Ext.get ('Button1').on
(gives function ())

Ext.get is a method of a 3rd party libary (I'm guessing "Ext" :-) that
returns something else than a DOM reference which does have an "on"
method.

I'd suggest as a JavaScript beginner, you should avoid using 3rd party
libraries until you know what you're doing, or at least read their
documentation.

With the DOM API events are set either directly:

document.getElementById('Button1').onclick = function() { ... }

or with addEventListener:

document.getElementById('Button1').addEventListene r("click",
function() { ... }, true);

Which IE however doesn't support. You need to use attachEvent for IE.
More details at:
http://developer.mozilla.org/en/docs...dEventListener
Thanks for the head up regarding IE. Also, thanks for the
very insightful reply.

--
Regards
Konrad Viltersten


Reply With Quote
  #6  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: The mothod "on" not available for a button?! - 05-23-2008 , 05:49 AM



Henry wrote:
Quote:
On May 23, 11:08 am, K Viltersten wrote:
document.getElementById ('Button1').on
(gives nothing - no such method)

while

Ext.get ('Button1').on
(gives function ())

What do i miss?

That - document.getElementById - returns an element from the DOM and -
Ext.get -(whatever that is) either returns a different sort of object
or returns a DOM element that has been subject to (direct or indirect)
augmentation prior to being returned.
And it should also be noted again that direct augmentation is inherently
error-prone as host objects may implement their own internal [[Put]]
algorithm allowed per the ECMAScript Specification. And so it can be
observed that often code written by clueless/inexperienced people directly
augments host objects, they not being aware that their code may break any
minute.


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


Reply With Quote
  #7  
Old   
Stevo
 
Posts: n/a

Default Re: The mothod "on" not available for a button?! - 05-23-2008 , 05:56 AM



Thomas 'PointedEars' Lahn wrote:
Quote:
document.getElementById('Button1').addEventListene r("click",

Reference Worms[tm] are error-prone, and should therefore be avoided.

PointedEars
Reference worms. I like it. Did you make that up ? I've always wanted a
term to describe what that code above is doing (incorrectly assuming
that Button1 will be returned as an object and disregarding the
possibility that it might return null / undefined).


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.