Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #36182 > unrolled thread

Event handler/listener syntax

Started byJonas Thörnvall <jonas.thornvall@gmail.com>
First post2019-03-27 00:48 -0700
Last post2019-03-27 01:03 -0700
Articles 4 — 1 participant

Back to article view | Back to comp.lang.javascript


Contents

  Event handler/listener syntax Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-03-27 00:48 -0700
    Re: Event handler/listener syntax Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-03-27 00:54 -0700
      Re: Event handler/listener syntax Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-03-27 00:57 -0700
        Re: Event handler/listener syntax Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-03-27 01:03 -0700

#36182 — Event handler/listener syntax

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2019-03-27 00:48 -0700
SubjectEvent handler/listener syntax
Message-ID<10e6bc28-bec2-4796-81af-a4159fc0cb96@googlegroups.com>
I find the eventhandler syntax weird "constructed" and hard to read, can you break out the actual code to to from eventhandler?

document.getElementById("try").addEventListener("click", function(){ 
   
}); 


instead

document.getElementById("try").addEventListener("click", dothis);



function dothis(){
   document.getElementById("text").innerText = "GeeksforGeeks"; 
}

If not why was the former syntax chosen?

[toc] | [next] | [standalone]


#36184

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2019-03-27 00:54 -0700
Message-ID<06177936-cef7-4b93-8441-38c9c4b76781@googlegroups.com>
In reply to#36182
Den onsdag 27 mars 2019 kl. 08:48:43 UTC+1 skrev Jonas Thörnvall:
> I find the eventhandler syntax weird "constructed" and hard to read, can you break out the actual code to to from eventhandler?
> 
> document.getElementById("try").addEventListener("click", function(){ 
>    
> }); 
> 
> 
> instead
> 
> document.getElementById("try").addEventListener("click", dothis);
> 
> 
> 
> function dothis(){
>    document.getElementById("text").innerText = "GeeksforGeeks"; 
> }
> 
> If not why was the former syntax chosen?

Apparently i am not the only one finding the syntax a bit weird.

https://stackoverflow.com/questions/16310423/addeventlistener-calls-the-function-without-me-even-asking-it-to

[toc] | [prev] | [next] | [standalone]


#36185

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2019-03-27 00:57 -0700
Message-ID<e8b78c13-42d4-4aaa-8898-1524106989fd@googlegroups.com>
In reply to#36184
Den onsdag 27 mars 2019 kl. 08:54:14 UTC+1 skrev Jonas Thörnvall:
> Den onsdag 27 mars 2019 kl. 08:48:43 UTC+1 skrev Jonas Thörnvall:
> > I find the eventhandler syntax weird "constructed" and hard to read, can you break out the actual code to to from eventhandler?
> > 
> > document.getElementById("try").addEventListener("click", function(){ 
> >    
> > }); 
> > 
> > 
> > instead
> > 
> > document.getElementById("try").addEventListener("click", dothis);
> > 
> > 
> > 
> > function dothis(){
> >    document.getElementById("text").innerText = "GeeksforGeeks"; 
> > }
> > 
> > If not why was the former syntax chosen?
> 
> Apparently i am not the only one finding the syntax a bit weird.
> 
> https://stackoverflow.com/questions/16310423/addeventlistener-calls-the-function-without-me-even-asking-it-to

Isn't this the natural way to do it????
function message_me(m_text){
    alert(m_text)
}

second.addEventListener('click', message_me('shazam'))

[toc] | [prev] | [next] | [standalone]


#36186

FromJonas Thörnvall <jonas.thornvall@gmail.com>
Date2019-03-27 01:03 -0700
Message-ID<4186fe48-5403-42db-92fc-6f3e4e1aef74@googlegroups.com>
In reply to#36185
Den onsdag 27 mars 2019 kl. 08:57:30 UTC+1 skrev Jonas Thörnvall:
> Den onsdag 27 mars 2019 kl. 08:54:14 UTC+1 skrev Jonas Thörnvall:
> > Den onsdag 27 mars 2019 kl. 08:48:43 UTC+1 skrev Jonas Thörnvall:
> > > I find the eventhandler syntax weird "constructed" and hard to read, can you break out the actual code to to from eventhandler?
> > > 
> > > document.getElementById("try").addEventListener("click", function(){ 
> > >    
> > > }); 
> > > 
> > > 
> > > instead
> > > 
> > > document.getElementById("try").addEventListener("click", dothis);
> > > 
> > > 
> > > 
> > > function dothis(){
> > >    document.getElementById("text").innerText = "GeeksforGeeks"; 
> > > }
> > > 
> > > If not why was the former syntax chosen?
> > 
> > Apparently i am not the only one finding the syntax a bit weird.
> > 
> > https://stackoverflow.com/questions/16310423/addeventlistener-calls-the-function-without-me-even-asking-it-to
> 
> Isn't this the natural way to do it????
> function message_me(m_text){
>     alert(m_text)
> }
> 
> second.addEventListener('click', message_me('shazam'))

Why the strange wrapping layer when you want todo something simple like?

function checkIfCharNotes(){
charNotes = document.getElementById("keyboardlistener").checked;

if (charNotes){ document.addEventListener("keydown", ListenKeybNote())}
else {document.addEventListener('keydown',nada());}
}

Because this is natural.
But the real syntax to do it is just weird? And i wonder how where they thinking.




[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web