0

Does something like an onevent event listener exist? I would like to do something like this:

window.addEventListener("event", function(event) {
  console.log(event.type);
});
RamenChef
  • 5,557
  • 11
  • 31
  • 43
Matija
  • 493
  • 4
  • 9
  • 21
  • Please review [ask] and elaborate on what you want, what you've tried, and why it didn't work. – zzzzBov Dec 01 '16 at 18:11
  • Possible duplicate of [Correct usage of addEventListener() / attachEvent()?](http://stackoverflow.com/questions/2657182/correct-usage-of-addeventlistener-attachevent) –  Dec 01 '16 at 18:12
  • I want to know does event listener "onevent" or "event" or something like that – Matija Dec 01 '16 at 18:14
  • No, not like that, you set the event you want to catch, not _an event_ as in any ... how is the browser suppose to know which one you want? – Asons Dec 01 '16 at 18:15
  • Possible duplicate of [How can I bind all events on a DOM element?](http://stackoverflow.com/questions/5848598/how-can-i-bind-all-events-on-a-dom-element) – Alex Young Dec 01 '16 at 18:16
  • ok,is there a way to log to console event.type everytime any event happens(this is now tottaly different question but the purpose of the first question was to get answer for this qestion) – Matija Dec 01 '16 at 18:18
  • http://stackoverflow.com/questions/7439570/how-do-you-log-all-events-fired-by-an-element-in-jquery – Luigi Cerone Dec 01 '16 at 18:23
  • @Matija — I think you are asking "Is it possible to set an event listener that will trigger for any event, no matter if it is a click, a blur, a load or something else?". Could you edit the question (not just commenting below) to clarify? – Quentin Dec 01 '16 at 18:51

1 Answers1

-1

in the event you want logged you can just add "e" in the fuction and do console.log(e); for example $('#theEvent').on("blur",function(e){ console.log(e); }); and if you want to catch a certain event you can do

$('#theEvent').on("blur click",function(e){
        if(e.type === "click"){
        console.log(e.type + "The user did click");
    }else{
          console.log(e.type + "The user did blur");
         }
         });

An other way to do it with case switch

$('#theEvent').on("blur click focus",function(e){
    switch(e.type){
        case "click":   
            console.log("The user clicked.");
            break;
        case "blur":
            console.log("The user blurred.");
            break;
        case "focus":
            console.log("The user focused.");
            break;
    }   
});
<!-- html sample -->


<input type='text' id="theEvent"/>

This is a random example but i think you get the idea you can do alot of if statements, so when the right event was triggered it will run the code

NotanNimda
  • 407
  • 1
  • 3
  • 11