addEventListener after fetched data

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












This is working code, but I want to simplify, correct, and improve the code.



window.addEvent = function(elem,type,callback) 
var evt = function(e)
return callback.call(elem,e);
;
var cb = function(e) return evt(e); ;
elem.addEventListener(type,cb,false);
return elem;
;
window.findParent = function(child,filter,root)
do
if( filter(child)) return child;
if( root && child == root) return false;
while(child = child.parentNode);
return false;
;
function on(type, target, callback)
window.addEvent(document.body, type, function(e)
var s = window.findParent(e.srcElement );

on("click", "page-link", function(e)
console.log("hey its working");
);


In this example, I update the pagination with fetch, and I have to bind addEventlistener to every page link. I am thinking about, If its there a cleaner way, using the latest techniques? (with pure Javascript)







share|improve this question



























    up vote
    2
    down vote

    favorite












    This is working code, but I want to simplify, correct, and improve the code.



    window.addEvent = function(elem,type,callback) 
    var evt = function(e)
    return callback.call(elem,e);
    ;
    var cb = function(e) return evt(e); ;
    elem.addEventListener(type,cb,false);
    return elem;
    ;
    window.findParent = function(child,filter,root)
    do
    if( filter(child)) return child;
    if( root && child == root) return false;
    while(child = child.parentNode);
    return false;
    ;
    function on(type, target, callback)
    window.addEvent(document.body, type, function(e)
    var s = window.findParent(e.srcElement );

    on("click", "page-link", function(e)
    console.log("hey its working");
    );


    In this example, I update the pagination with fetch, and I have to bind addEventlistener to every page link. I am thinking about, If its there a cleaner way, using the latest techniques? (with pure Javascript)







    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      This is working code, but I want to simplify, correct, and improve the code.



      window.addEvent = function(elem,type,callback) 
      var evt = function(e)
      return callback.call(elem,e);
      ;
      var cb = function(e) return evt(e); ;
      elem.addEventListener(type,cb,false);
      return elem;
      ;
      window.findParent = function(child,filter,root)
      do
      if( filter(child)) return child;
      if( root && child == root) return false;
      while(child = child.parentNode);
      return false;
      ;
      function on(type, target, callback)
      window.addEvent(document.body, type, function(e)
      var s = window.findParent(e.srcElement );

      on("click", "page-link", function(e)
      console.log("hey its working");
      );


      In this example, I update the pagination with fetch, and I have to bind addEventlistener to every page link. I am thinking about, If its there a cleaner way, using the latest techniques? (with pure Javascript)







      share|improve this question













      This is working code, but I want to simplify, correct, and improve the code.



      window.addEvent = function(elem,type,callback) 
      var evt = function(e)
      return callback.call(elem,e);
      ;
      var cb = function(e) return evt(e); ;
      elem.addEventListener(type,cb,false);
      return elem;
      ;
      window.findParent = function(child,filter,root)
      do
      if( filter(child)) return child;
      if( root && child == root) return false;
      while(child = child.parentNode);
      return false;
      ;
      function on(type, target, callback)
      window.addEvent(document.body, type, function(e)
      var s = window.findParent(e.srcElement );

      on("click", "page-link", function(e)
      console.log("hey its working");
      );


      In this example, I update the pagination with fetch, and I have to bind addEventlistener to every page link. I am thinking about, If its there a cleaner way, using the latest techniques? (with pure Javascript)









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jan 30 at 5:13









      Sam Onela

      5,88461545




      5,88461545









      asked Jan 29 at 21:51









      almost okey

      164




      164




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Response to statement




          In this example, I update the pagination with fetch, and I have to bind addEventlistener to every page link.




          Actually, in the example code it appears that elem.addEventListener() is called on document.body, not each link.



          Variable Names



          Some of the names are a bit misleading. For example target is used to check the class name of elements. Therefore a better name might be targetClass, target_class, etc. Similarly, s isn't very descriptive.



          Feedback



          Traversing DOM



          Let's look at the findParent() function:




          window.findParent = function(child,filter,root) 
          do
          if( filter(child)) return child;
          if( root && child == root) return false;
          while(child = child.parentNode);
          return false;
          ;



          When would root ever evaluate to a false-y value? In the code above, document.body is passed, so that seems unlikely.



          It might be simpler to use a while loop. Typically DOM traversing code I have seen would assign the child argument to a temporary variable (e.g. currentNode) and assign the parent node to that variable until the target node is found or the root is reached.



          window.findParent = function(child,filter,root) 
          var currentNode = child;
          while(root != currentNode)
          if (filter(currentNode ))
          return currentNode ;

          currentNode = currentNode.parentNode;

          return false;



          This could also be re-written as a for loop:



          window.findParent = function(child,filter,root) {
          for(var currentNode = child; root != currentNode; currentNode = curentNode.parentNode)
          if (filter(currentNode ))
          return currentNode ;


          return false;


          Over-complicated addEvent



          The addEvent function creates two functions (i.e. evt and cb) which basically just call callback passing the event object to the callback. These steps are superfluous. It could simply add the event listener, passing cb to the listener parameter:



          window.addEvent = function(elem,type,callback) 
          elem.addEventListener(type,callback,false);
          return elem;
          ;


          And does it need to return elem? That would support chaining but your example code doesn't assign that return value to anything or have a subsequent call...



          Simplification



          This looks like a great application for an event delegate, unless there is some requirement I am missing about splitting out the functionality into those separate functions? See the example below, which adds the event handler to the root and then only calls the callback if the target of the event passes the filter.






          function on(eventType, targetClass, callback) 
          document.body.addEventListener(eventType, function (event)
          if (event.target.classList.contains(targetClass))
          callback(event);

          );


          on("click", "page-link", function(e)
          console.log("hey its working", e.target.innerHTML);
          );

          <h1>Header Text</h1>
          <a class="page-link" href="javascript:void(0);">1</a>
          <a class="page-link" href="javascript:void(0);">2</a>
          <a class="page-link" href="javascript:void(0);">3</a>








          share|improve this answer























          • this simplification made my day, there was no reason to split the code into multiple functions, I think it was easy to write. Thank you for the well explained answer, now I understand better.
            – almost okey
            Jan 30 at 13:16

















          up vote
          3
          down vote













          I think you can use querySelectorAll() - for example:



          function on (type, target, callback) 
          document.querySelectorAll(target).forEach(function (elem)
          elem.addEventListener(type, callback);
          )



          Your function findParent seems similar to element.closest() . In case you need it to work with IE, you can use the code below. It works with a selector, not just a class name.



          closest = (element, selector) => 
          while(element)
          //For IE11
          if (!element.matches)
          element.matches = element.msMatchesSelector;

          if (element.matches(selector))
          return element;

          element = element.parentElement;







          share|improve this answer























          • with the sample usage of on(), document.querySelectorAll() would likely return no results when passed "page-link" unless there were elements with that tag name...
            – Sam Onela
            Jan 30 at 4:52










          • @SamOnela because in your findParent you find element using classList. You can change to ".page-link". Just my opinion, because pass and work with selector is better and flexible than just only 1 class.
            – DarknessZX
            Jan 30 at 4:57










          • I am just saying that the OP would have to change the original sample code... or to use the code you suggested, would have to alter it to something like: document.querySelectorAll("." + target)
            – Sam Onela
            Jan 30 at 5:00










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186284%2faddeventlistener-after-fetched-data%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          Response to statement




          In this example, I update the pagination with fetch, and I have to bind addEventlistener to every page link.




          Actually, in the example code it appears that elem.addEventListener() is called on document.body, not each link.



          Variable Names



          Some of the names are a bit misleading. For example target is used to check the class name of elements. Therefore a better name might be targetClass, target_class, etc. Similarly, s isn't very descriptive.



          Feedback



          Traversing DOM



          Let's look at the findParent() function:




          window.findParent = function(child,filter,root) 
          do
          if( filter(child)) return child;
          if( root && child == root) return false;
          while(child = child.parentNode);
          return false;
          ;



          When would root ever evaluate to a false-y value? In the code above, document.body is passed, so that seems unlikely.



          It might be simpler to use a while loop. Typically DOM traversing code I have seen would assign the child argument to a temporary variable (e.g. currentNode) and assign the parent node to that variable until the target node is found or the root is reached.



          window.findParent = function(child,filter,root) 
          var currentNode = child;
          while(root != currentNode)
          if (filter(currentNode ))
          return currentNode ;

          currentNode = currentNode.parentNode;

          return false;



          This could also be re-written as a for loop:



          window.findParent = function(child,filter,root) {
          for(var currentNode = child; root != currentNode; currentNode = curentNode.parentNode)
          if (filter(currentNode ))
          return currentNode ;


          return false;


          Over-complicated addEvent



          The addEvent function creates two functions (i.e. evt and cb) which basically just call callback passing the event object to the callback. These steps are superfluous. It could simply add the event listener, passing cb to the listener parameter:



          window.addEvent = function(elem,type,callback) 
          elem.addEventListener(type,callback,false);
          return elem;
          ;


          And does it need to return elem? That would support chaining but your example code doesn't assign that return value to anything or have a subsequent call...



          Simplification



          This looks like a great application for an event delegate, unless there is some requirement I am missing about splitting out the functionality into those separate functions? See the example below, which adds the event handler to the root and then only calls the callback if the target of the event passes the filter.






          function on(eventType, targetClass, callback) 
          document.body.addEventListener(eventType, function (event)
          if (event.target.classList.contains(targetClass))
          callback(event);

          );


          on("click", "page-link", function(e)
          console.log("hey its working", e.target.innerHTML);
          );

          <h1>Header Text</h1>
          <a class="page-link" href="javascript:void(0);">1</a>
          <a class="page-link" href="javascript:void(0);">2</a>
          <a class="page-link" href="javascript:void(0);">3</a>








          share|improve this answer























          • this simplification made my day, there was no reason to split the code into multiple functions, I think it was easy to write. Thank you for the well explained answer, now I understand better.
            – almost okey
            Jan 30 at 13:16














          up vote
          1
          down vote



          accepted










          Response to statement




          In this example, I update the pagination with fetch, and I have to bind addEventlistener to every page link.




          Actually, in the example code it appears that elem.addEventListener() is called on document.body, not each link.



          Variable Names



          Some of the names are a bit misleading. For example target is used to check the class name of elements. Therefore a better name might be targetClass, target_class, etc. Similarly, s isn't very descriptive.



          Feedback



          Traversing DOM



          Let's look at the findParent() function:




          window.findParent = function(child,filter,root) 
          do
          if( filter(child)) return child;
          if( root && child == root) return false;
          while(child = child.parentNode);
          return false;
          ;



          When would root ever evaluate to a false-y value? In the code above, document.body is passed, so that seems unlikely.



          It might be simpler to use a while loop. Typically DOM traversing code I have seen would assign the child argument to a temporary variable (e.g. currentNode) and assign the parent node to that variable until the target node is found or the root is reached.



          window.findParent = function(child,filter,root) 
          var currentNode = child;
          while(root != currentNode)
          if (filter(currentNode ))
          return currentNode ;

          currentNode = currentNode.parentNode;

          return false;



          This could also be re-written as a for loop:



          window.findParent = function(child,filter,root) {
          for(var currentNode = child; root != currentNode; currentNode = curentNode.parentNode)
          if (filter(currentNode ))
          return currentNode ;


          return false;


          Over-complicated addEvent



          The addEvent function creates two functions (i.e. evt and cb) which basically just call callback passing the event object to the callback. These steps are superfluous. It could simply add the event listener, passing cb to the listener parameter:



          window.addEvent = function(elem,type,callback) 
          elem.addEventListener(type,callback,false);
          return elem;
          ;


          And does it need to return elem? That would support chaining but your example code doesn't assign that return value to anything or have a subsequent call...



          Simplification



          This looks like a great application for an event delegate, unless there is some requirement I am missing about splitting out the functionality into those separate functions? See the example below, which adds the event handler to the root and then only calls the callback if the target of the event passes the filter.






          function on(eventType, targetClass, callback) 
          document.body.addEventListener(eventType, function (event)
          if (event.target.classList.contains(targetClass))
          callback(event);

          );


          on("click", "page-link", function(e)
          console.log("hey its working", e.target.innerHTML);
          );

          <h1>Header Text</h1>
          <a class="page-link" href="javascript:void(0);">1</a>
          <a class="page-link" href="javascript:void(0);">2</a>
          <a class="page-link" href="javascript:void(0);">3</a>








          share|improve this answer























          • this simplification made my day, there was no reason to split the code into multiple functions, I think it was easy to write. Thank you for the well explained answer, now I understand better.
            – almost okey
            Jan 30 at 13:16












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Response to statement




          In this example, I update the pagination with fetch, and I have to bind addEventlistener to every page link.




          Actually, in the example code it appears that elem.addEventListener() is called on document.body, not each link.



          Variable Names



          Some of the names are a bit misleading. For example target is used to check the class name of elements. Therefore a better name might be targetClass, target_class, etc. Similarly, s isn't very descriptive.



          Feedback



          Traversing DOM



          Let's look at the findParent() function:




          window.findParent = function(child,filter,root) 
          do
          if( filter(child)) return child;
          if( root && child == root) return false;
          while(child = child.parentNode);
          return false;
          ;



          When would root ever evaluate to a false-y value? In the code above, document.body is passed, so that seems unlikely.



          It might be simpler to use a while loop. Typically DOM traversing code I have seen would assign the child argument to a temporary variable (e.g. currentNode) and assign the parent node to that variable until the target node is found or the root is reached.



          window.findParent = function(child,filter,root) 
          var currentNode = child;
          while(root != currentNode)
          if (filter(currentNode ))
          return currentNode ;

          currentNode = currentNode.parentNode;

          return false;



          This could also be re-written as a for loop:



          window.findParent = function(child,filter,root) {
          for(var currentNode = child; root != currentNode; currentNode = curentNode.parentNode)
          if (filter(currentNode ))
          return currentNode ;


          return false;


          Over-complicated addEvent



          The addEvent function creates two functions (i.e. evt and cb) which basically just call callback passing the event object to the callback. These steps are superfluous. It could simply add the event listener, passing cb to the listener parameter:



          window.addEvent = function(elem,type,callback) 
          elem.addEventListener(type,callback,false);
          return elem;
          ;


          And does it need to return elem? That would support chaining but your example code doesn't assign that return value to anything or have a subsequent call...



          Simplification



          This looks like a great application for an event delegate, unless there is some requirement I am missing about splitting out the functionality into those separate functions? See the example below, which adds the event handler to the root and then only calls the callback if the target of the event passes the filter.






          function on(eventType, targetClass, callback) 
          document.body.addEventListener(eventType, function (event)
          if (event.target.classList.contains(targetClass))
          callback(event);

          );


          on("click", "page-link", function(e)
          console.log("hey its working", e.target.innerHTML);
          );

          <h1>Header Text</h1>
          <a class="page-link" href="javascript:void(0);">1</a>
          <a class="page-link" href="javascript:void(0);">2</a>
          <a class="page-link" href="javascript:void(0);">3</a>








          share|improve this answer















          Response to statement




          In this example, I update the pagination with fetch, and I have to bind addEventlistener to every page link.




          Actually, in the example code it appears that elem.addEventListener() is called on document.body, not each link.



          Variable Names



          Some of the names are a bit misleading. For example target is used to check the class name of elements. Therefore a better name might be targetClass, target_class, etc. Similarly, s isn't very descriptive.



          Feedback



          Traversing DOM



          Let's look at the findParent() function:




          window.findParent = function(child,filter,root) 
          do
          if( filter(child)) return child;
          if( root && child == root) return false;
          while(child = child.parentNode);
          return false;
          ;



          When would root ever evaluate to a false-y value? In the code above, document.body is passed, so that seems unlikely.



          It might be simpler to use a while loop. Typically DOM traversing code I have seen would assign the child argument to a temporary variable (e.g. currentNode) and assign the parent node to that variable until the target node is found or the root is reached.



          window.findParent = function(child,filter,root) 
          var currentNode = child;
          while(root != currentNode)
          if (filter(currentNode ))
          return currentNode ;

          currentNode = currentNode.parentNode;

          return false;



          This could also be re-written as a for loop:



          window.findParent = function(child,filter,root) {
          for(var currentNode = child; root != currentNode; currentNode = curentNode.parentNode)
          if (filter(currentNode ))
          return currentNode ;


          return false;


          Over-complicated addEvent



          The addEvent function creates two functions (i.e. evt and cb) which basically just call callback passing the event object to the callback. These steps are superfluous. It could simply add the event listener, passing cb to the listener parameter:



          window.addEvent = function(elem,type,callback) 
          elem.addEventListener(type,callback,false);
          return elem;
          ;


          And does it need to return elem? That would support chaining but your example code doesn't assign that return value to anything or have a subsequent call...



          Simplification



          This looks like a great application for an event delegate, unless there is some requirement I am missing about splitting out the functionality into those separate functions? See the example below, which adds the event handler to the root and then only calls the callback if the target of the event passes the filter.






          function on(eventType, targetClass, callback) 
          document.body.addEventListener(eventType, function (event)
          if (event.target.classList.contains(targetClass))
          callback(event);

          );


          on("click", "page-link", function(e)
          console.log("hey its working", e.target.innerHTML);
          );

          <h1>Header Text</h1>
          <a class="page-link" href="javascript:void(0);">1</a>
          <a class="page-link" href="javascript:void(0);">2</a>
          <a class="page-link" href="javascript:void(0);">3</a>








          function on(eventType, targetClass, callback) 
          document.body.addEventListener(eventType, function (event)
          if (event.target.classList.contains(targetClass))
          callback(event);

          );


          on("click", "page-link", function(e)
          console.log("hey its working", e.target.innerHTML);
          );

          <h1>Header Text</h1>
          <a class="page-link" href="javascript:void(0);">1</a>
          <a class="page-link" href="javascript:void(0);">2</a>
          <a class="page-link" href="javascript:void(0);">3</a>





          function on(eventType, targetClass, callback) 
          document.body.addEventListener(eventType, function (event)
          if (event.target.classList.contains(targetClass))
          callback(event);

          );


          on("click", "page-link", function(e)
          console.log("hey its working", e.target.innerHTML);
          );

          <h1>Header Text</h1>
          <a class="page-link" href="javascript:void(0);">1</a>
          <a class="page-link" href="javascript:void(0);">2</a>
          <a class="page-link" href="javascript:void(0);">3</a>






          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Jan 30 at 16:53


























          answered Jan 30 at 5:10









          Sam Onela

          5,88461545




          5,88461545











          • this simplification made my day, there was no reason to split the code into multiple functions, I think it was easy to write. Thank you for the well explained answer, now I understand better.
            – almost okey
            Jan 30 at 13:16
















          • this simplification made my day, there was no reason to split the code into multiple functions, I think it was easy to write. Thank you for the well explained answer, now I understand better.
            – almost okey
            Jan 30 at 13:16















          this simplification made my day, there was no reason to split the code into multiple functions, I think it was easy to write. Thank you for the well explained answer, now I understand better.
          – almost okey
          Jan 30 at 13:16




          this simplification made my day, there was no reason to split the code into multiple functions, I think it was easy to write. Thank you for the well explained answer, now I understand better.
          – almost okey
          Jan 30 at 13:16












          up vote
          3
          down vote













          I think you can use querySelectorAll() - for example:



          function on (type, target, callback) 
          document.querySelectorAll(target).forEach(function (elem)
          elem.addEventListener(type, callback);
          )



          Your function findParent seems similar to element.closest() . In case you need it to work with IE, you can use the code below. It works with a selector, not just a class name.



          closest = (element, selector) => 
          while(element)
          //For IE11
          if (!element.matches)
          element.matches = element.msMatchesSelector;

          if (element.matches(selector))
          return element;

          element = element.parentElement;







          share|improve this answer























          • with the sample usage of on(), document.querySelectorAll() would likely return no results when passed "page-link" unless there were elements with that tag name...
            – Sam Onela
            Jan 30 at 4:52










          • @SamOnela because in your findParent you find element using classList. You can change to ".page-link". Just my opinion, because pass and work with selector is better and flexible than just only 1 class.
            – DarknessZX
            Jan 30 at 4:57










          • I am just saying that the OP would have to change the original sample code... or to use the code you suggested, would have to alter it to something like: document.querySelectorAll("." + target)
            – Sam Onela
            Jan 30 at 5:00














          up vote
          3
          down vote













          I think you can use querySelectorAll() - for example:



          function on (type, target, callback) 
          document.querySelectorAll(target).forEach(function (elem)
          elem.addEventListener(type, callback);
          )



          Your function findParent seems similar to element.closest() . In case you need it to work with IE, you can use the code below. It works with a selector, not just a class name.



          closest = (element, selector) => 
          while(element)
          //For IE11
          if (!element.matches)
          element.matches = element.msMatchesSelector;

          if (element.matches(selector))
          return element;

          element = element.parentElement;







          share|improve this answer























          • with the sample usage of on(), document.querySelectorAll() would likely return no results when passed "page-link" unless there were elements with that tag name...
            – Sam Onela
            Jan 30 at 4:52










          • @SamOnela because in your findParent you find element using classList. You can change to ".page-link". Just my opinion, because pass and work with selector is better and flexible than just only 1 class.
            – DarknessZX
            Jan 30 at 4:57










          • I am just saying that the OP would have to change the original sample code... or to use the code you suggested, would have to alter it to something like: document.querySelectorAll("." + target)
            – Sam Onela
            Jan 30 at 5:00












          up vote
          3
          down vote










          up vote
          3
          down vote









          I think you can use querySelectorAll() - for example:



          function on (type, target, callback) 
          document.querySelectorAll(target).forEach(function (elem)
          elem.addEventListener(type, callback);
          )



          Your function findParent seems similar to element.closest() . In case you need it to work with IE, you can use the code below. It works with a selector, not just a class name.



          closest = (element, selector) => 
          while(element)
          //For IE11
          if (!element.matches)
          element.matches = element.msMatchesSelector;

          if (element.matches(selector))
          return element;

          element = element.parentElement;







          share|improve this answer















          I think you can use querySelectorAll() - for example:



          function on (type, target, callback) 
          document.querySelectorAll(target).forEach(function (elem)
          elem.addEventListener(type, callback);
          )



          Your function findParent seems similar to element.closest() . In case you need it to work with IE, you can use the code below. It works with a selector, not just a class name.



          closest = (element, selector) => 
          while(element)
          //For IE11
          if (!element.matches)
          element.matches = element.msMatchesSelector;

          if (element.matches(selector))
          return element;

          element = element.parentElement;








          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Jan 30 at 16:36









          Sam Onela

          5,88461545




          5,88461545











          answered Jan 30 at 4:48









          DarknessZX

          1313




          1313











          • with the sample usage of on(), document.querySelectorAll() would likely return no results when passed "page-link" unless there were elements with that tag name...
            – Sam Onela
            Jan 30 at 4:52










          • @SamOnela because in your findParent you find element using classList. You can change to ".page-link". Just my opinion, because pass and work with selector is better and flexible than just only 1 class.
            – DarknessZX
            Jan 30 at 4:57










          • I am just saying that the OP would have to change the original sample code... or to use the code you suggested, would have to alter it to something like: document.querySelectorAll("." + target)
            – Sam Onela
            Jan 30 at 5:00
















          • with the sample usage of on(), document.querySelectorAll() would likely return no results when passed "page-link" unless there were elements with that tag name...
            – Sam Onela
            Jan 30 at 4:52










          • @SamOnela because in your findParent you find element using classList. You can change to ".page-link". Just my opinion, because pass and work with selector is better and flexible than just only 1 class.
            – DarknessZX
            Jan 30 at 4:57










          • I am just saying that the OP would have to change the original sample code... or to use the code you suggested, would have to alter it to something like: document.querySelectorAll("." + target)
            – Sam Onela
            Jan 30 at 5:00















          with the sample usage of on(), document.querySelectorAll() would likely return no results when passed "page-link" unless there were elements with that tag name...
          – Sam Onela
          Jan 30 at 4:52




          with the sample usage of on(), document.querySelectorAll() would likely return no results when passed "page-link" unless there were elements with that tag name...
          – Sam Onela
          Jan 30 at 4:52












          @SamOnela because in your findParent you find element using classList. You can change to ".page-link". Just my opinion, because pass and work with selector is better and flexible than just only 1 class.
          – DarknessZX
          Jan 30 at 4:57




          @SamOnela because in your findParent you find element using classList. You can change to ".page-link". Just my opinion, because pass and work with selector is better and flexible than just only 1 class.
          – DarknessZX
          Jan 30 at 4:57












          I am just saying that the OP would have to change the original sample code... or to use the code you suggested, would have to alter it to something like: document.querySelectorAll("." + target)
          – Sam Onela
          Jan 30 at 5:00




          I am just saying that the OP would have to change the original sample code... or to use the code you suggested, would have to alter it to something like: document.querySelectorAll("." + target)
          – Sam Onela
          Jan 30 at 5:00












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186284%2faddeventlistener-after-fetched-data%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Python Lists

          Aion

          JavaScript Array Iteration Methods