0



I'm new to JQuery and I'm messing it up...
I'm trying to build a tour for my website. I have this function for building and showing the popovers. They do show up, but the buttons just work in the first popover (with only one button).

This is the code I have right now:

function createButton(i){ //Creates buttons for later appending to popover's content
    if (i==0){ //First popover, just needs "Next" button
        return '<a class="btn btn-light border border-dark float-right mb-2" >Next</a>';
    }

    else if(i<popovers.length-1){ //Popovers in the middle need both "Previous" and "Next" buttons
        var buttons = new Array();
        buttons[0] = '<a class="btn btn-light border border-dark float-left mb-2">Previous</a>';
        buttons[1] = '<a class="btn btn-light border border-dark float-right mb-2">Next</a>';
        return buttons;
    }

    else{ //The last popover only needs a "Finish" button
        return '<a class="btn btn-light border border-dark float-right mb-2">Finish</a>';
    }
}

The problem must be here:

function showPopover(i){

        var current = popovers[i];
        var button = $(createButton(i));
        current.popover('toggle');
        var new_position = $('.popover').offset();
        var content = $('.popover-body');

        if(button.length == 2){
            $(button[0]).appendTo(content);
            $(button[1]).appendTo(content);
        }
        else{
            button.appendTo(content);
        }
        window.scrollTo( new_position.left, new_position.top - 60 );

        if(button.length == 2){

            $(button[0]).click(function ()
            {
                current.popover('toggle');
                content.detach();
                i--;
                if(i>=0){
                    showPopover(i);
                }
            });

            $(button[1]).click(function ()
            {
                current.popover('toggle');
                content.detach();
                i++;
                if(i!=popovers.length){
                    showPopover(i);
                }
            });
        }
        else{
            button.click(function ()
            {
                current.popover( 'toggle' );
                content.detach();
                i++;
                if(i!=popovers.length){
                    showPopover(i);
                }
            });
        }


    };

While inspecting the buttons, I could see that there's no event attached to them, so the problem is in the click functions.
I thought I might be using wrongly the JQuery selectors; however, the append functions do work and show the buttons on the popover.

Thanks for your answers!

1 Answer 1

1

I think first you append those buttons to the DOM and then only attach the events.

since I can see you are using $(button[0]) something. jQuery will search that element in DOM and attache the click event there. and on more thing button[0] is not an actual DOM object yet at that point it is just a string.

Sign up to request clarification or add additional context in comments.

2 Comments

So then how should I pass JQuery the DOM object? I didn't understand your point, sorry
Instead if $(button[0]) try $(".popover-body a:first-child").click... or if you could assigned some ids to your button then directly use $("#someId").click...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.