rmdez
rmdez

Reputation: 41

Cannot attach click event to buttons in JQuery



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!

Upvotes: 0

Views: 82

Answers (1)

Mujibur Rehman Ansari
Mujibur Rehman Ansari

Reputation: 663

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.

Upvotes: 1

Related Questions