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


Groups > comp.lang.javascript > #36185

Re: Event handler/listener syntax

Newsgroups comp.lang.javascript
Date 2019-03-27 00:57 -0700
References <10e6bc28-bec2-4796-81af-a4159fc0cb96@googlegroups.com> <06177936-cef7-4b93-8441-38c9c4b76781@googlegroups.com>
Message-ID <e8b78c13-42d4-4aaa-8898-1524106989fd@googlegroups.com> (permalink)
Subject Re: Event handler/listener syntax
From Jonas Thörnvall <jonas.thornvall@gmail.com>

Show all headers | View raw


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'))

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web