Zaki Mohammed
Zaki Mohammed

Reputation: 1017

How to make Bootstrap popover appears once on page load

I want to make bootstrap popover should appears once on the page load over a button (just to use it as an instruction) and after clicking the button it should get destroyed.

I want to popover on the button and gets user's attention every time they visit my page and drag their attention to click on it if they are new visitors.

I have made following code, the popovers appears on the page load but it works just like normal popover afterwards it doesn't get destroyed when i click button.

HTML:

<div class="container">
    <div class="row">
        <div class="col-sm-12">
            <h1>Hello World</h1>

            <p>Click on button to see Popover</p>

            <button type="button" id="example" class="btn btn-primary" rel="popover"
                data-content="Check this new option when you visit our website to catch the latest news!"
                data-original-title="This is new feature">pop
            </button>                      
        </div>
    </div>
</div>

JS:

function destroyNew(){
    $('#example').popover('destroy');
}

$(window).load(function () {
    $("#example").popover('show');
});

I have also modified the JS and tried in this way:

JS:

$("#example").on('click', function () {
        $('#example').popover('destroy');
    }
});

$(window).load(function () {
        $("#example").popover('show');
});

The above code doesn't make the popover at all, not on the page load neither normally.

I want some experts to help me out, Thanks in advance.

Upvotes: 1

Views: 5731

Answers (3)

Allie
Allie

Reputation: 11

For Bootstrap 4, this is currently how to get rid of it:

$(function() { 
    $("#element").popover('show');

    $("#element").on('click', function () {
        $("#element").popover('dispose'); 
    });
 });

see documentation here

Upvotes: 1

johannesMatevosyan
johannesMatevosyan

Reputation: 2208

This works for me. Later you can hide it after click. You can check jsfiddle here

$(function() { 

    $("#example").popover('show');

    $("#example").on('click', function () {
        $('#example').popover('destroy');
    });
 }); 

Upvotes: 3

BZC
BZC

Reputation: 468

You can use cookie. Add a checkbox in modal. I using my a project. Try?

$('#ppvr_dontshowagainmodal').change(function() {
if($('#ppvr_dontshowagainmodal').prop('checked') == true){
document.cookie="ppvr_dontshowagainmodal=1; expires=Thu, 18 Dec 2016 12:00:00 UTC";
}
else{
document.cookie="ppvr_dontshowagainmodal=0; expires=Thu, 18 Dec 2016 12:00:00 UTC";
}

});
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
    }
    return "";
}

function showModal()
{
if(getCookie("ppvr_dontshowagainmodal") !== "1"){
      $('#myModal').modal('show')
}
}
window.onload = showModal();

Upvotes: 0

Related Questions