.each() in pure Javascript

Asked

Viewed 557 times

4

On a platform of forums there is a pagination system, and I would like to make this pagination via Ajax, for this, I built the following function:

//<![CDATA[
var Pagination = (function(w, d) {
    var pagination = {

        init: function() {
            this.run();
        },

        run: function() {

            w.addEventListener('DOMContentLoaded', function() {
                var links = d.querySelectorAll('p.paging a[href^="/t"]');

                for (var i = 0; i < links.length; i++) {
                    links[i].addEventListener('click', function(event) {

                        event.preventDefault();

                        var link = this.getAttribute('href'),
                            xhr = new XMLHttpRequest();
                        xhr.onload = function() {
                            d.querySelector('.main').innerHTML = this.responseXML.querySelector('.main').innerHTML;
                        };
                        xhr.open('GET', link);
                        xhr.responseType = 'document';
                        xhr.send();
                    });
                }
            });

        }
    };

    pagination.init();

}(window, document));
//]]>

It works perfect until the first click. But if I click on more than one link the function does not work at all and the page is loaded normally.

I’m pretty sure the mistake is in my loop for(), i haven’t found a better way to make the Ajax function for each time I click on a link from that pagination.

If there’s any better way, I’m glad to know.

@Edit:

The structure of my HTML is like this:

<div class="main">
    <p class="paging">
        <a href="/topic/1">1</a>
        <a href="/topic/2">2</a>
        <a href="/topic/3">3</a>
    </p>
    <div class="topic">
        <div class="content">Conteúdo aqui.</div>
    </div>
</div>
  • 3

    Are you clicking on links that already existed on the page, or on links that were added after the first ajax call? P.S. at first glance, doesn’t seem to have anything wrong in your loop for.

  • @mgibsonbr They already exist on the page, they are created via PHP (which we do not have access to), usually by clicking it will redirect to the next page.. I have tried several ways but still have not achieved a satisfactory result. It works on the first click but stops working from the following clicks.. As if I was using Document.querySelector()

  • The console points out some error?

  • @Brunoaugusto, no, it shows no error. Neither Warning, nothing..

  • 3

    @waghcwb the links from .paging a are inside the .main?

  • 1

    Have you tried isolating the logic of this "class" to see if it works?

  • 1

    @Sergio, that was exactly it, buddy. I switched the selector to one that didn’t include the pagination inside it, and it worked perfectly. I appreciate the attention of all of you friends, you are awesome!

  • @waghcwb great! If you want to delete the question; or put HTML for me, or you can answer and maybe be useful for others.

  • @Sergio, made friend.. Best leave it to the content available to everyone, maybe someday someone will face the same problem.

  • @waghcwb, I got a question when you put HTML. You’re rewriting HTML inside the main every time you make an AJAX request, right? This will cause you to lose the Vent handlers... so or run pagination.init(); every success of AJAX or replaces only the content of .topic. What are you doing?

Show 5 more comments

1 answer

5


In this your code is replacing the menu each time you load the div .main. This causes Vent handlers to get lost.

I see two options:

change selector in ajax callback to change the right content. Replacing to:

d.querySelector('.topic').innerHTML = this.responseXML.querySelector('.topic').innerHTML;

and thus change only the relevant content and not break the event headphones placed by your pagination.run();

putting back the Vent hadlers each time they load new content, once rewriting elements of the DOM causes them to lose the Vent handlers they had grabbed. So in ajax callback you can use:

xhr.onload = function() {
    d.querySelector('.main').innerHTML = this.responseXML.querySelector('.main').innerHTML;
    pagination.init();  // ou pagination.run();
};
  • Hello, I have a similar question, and I was wondering if you could direct me? The var fbShareBtn = Document.querySelector('.fb-share'); is only returning the first share button, I use multiple Facebook share buttons on the same page (paging) and only the first one works. You can see all the code on this site: http://tedshd.logdown.com/posts/200979-javascript-custom-facebook-sharebutton

  • @Thiagojem the querySelector select only the first element you find. You must use querySelectorAll and then iterate the elements with a for, If you need help, ask a new question on the site and we’ll help you with whatever you’re missing.

  • I asked the question... could you see it here: Link to the question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.