www.webdeveloper.com
Results 1 to 3 of 3

Thread: addEventListener - change execution order

  1. #1
    Join Date
    Jun 2013
    Posts
    3

    addEventListener - change execution order

    Hello everyone,

    Let's suppose an element has some attached functions on keydown, which where added using the addEventListener function.

    I want to add my own function to be executed but move its execution to the top. In other words I want my function to be executed before any other.

    Changing the source and adding my own addEventListener first is not an option :/

    Has anyone faced anything a similar issue? Do you have any ideas on how to solve it?

    I would like to mention that ANY solution is acceptable even if it requires additional JS frameworks etc.

    Thanks in advance

  2. #2
    Join Date
    Mar 2009
    Posts
    467
    This may not help you as it comes off the top of my head, but you might try something like this which MUST run after all the other scripts have loaded and attached the onkeydown listeners for the element:

    Code:
    function afterBurner(){
       var myFirst, element, fnc;
       myFirst = function(e){
       //whatever you want to do before the other functions are called
      }
    
      element = document.getElementById("theElementId"); 
      fnc = element.onkeydown;
      element.onkeydown = function(e){
         myFirst(e);
         var z  = fnc(e);
         return z;
     }
    }

  3. #3
    Join Date
    Jun 2013
    Posts
    3
    I have tried a similar solution but the problem is that element.onkeydown is null (and keypress/keyup). It is indeed a textarea which already has a function bind to it and when the user presses ENTER it gets processed. What I want to do is to add something to the text written BEFORE it gets processed. Actually, exactly what your example was all about.

    But anything I use, I simply cannot get it to execute first. For instance, I simply make it display an alert box and by the time the alert shows up, the text is already processed.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles