I have a div when clicked it is supposed to trigger a popover and show a separate div and it works fine. The problem is after the popover is displayed when you click on the external div it removes the popover but after removing the popover when you click on the popover div to show the popover again you have to click twice in order to trigger it. Why does it need to be clicked twice? how can I fix this so that it always works with one click? Any help is appreciated. Thanks in advance
Update:
I tried using this to solve it but it doesn't work too. It completely doesn't work on bootstrap 5 but on bootstrap 4 its able to catch that the click didn't trigger the popover but the solution doesn't work. Tried solution:
$('body').on('hidden.bs.popover', function (e) {
$(e.target).data("bs.popover")._activeTrigger.click = true
});
$(function() {
$(".testDiv").popover({
container: 'body',
html: true,
placement: 'bottom',
sanitize: false,
content: function() {
$('.testDivCon').show()
$('.wifiInfoContent').append('<button >Click Me</button>')
return $('.wifiInfoContent').html();
}
});
});
$('.testDivCon').on('click', function() {
$('.popover').remove()
$('.testDivCon').hide()
})
$(document).on('click', '.testButton', function() {
console.log('clicked')
})
.testDivCon {
display: none;
position: absolute;
top: 0%;
left: 0%;
width: 100%;
height: 100%;
background-color: green;
}
.testDiv {
position: absolute;
top: 10%;
left: 20%;
width: 20%;
height: 15%;
background-color: black;
}
.wifiInfoContent {
display: none;
}
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj 3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4 1nzFpr53nxSS GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG 2QOK9T ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg OMhuP IlRH9sENBO0LRn5q 8nbTov4 1p" crossorigin="anonymous"></script>
<div ></div>
<div ></div>
<div ></div>
CodePudding user response:
You need to correctly remove the popover:
$('.testDiv').popover('hide');
$(function() {
$(".testDiv").popover({
container: 'body',
html: true,
placement: 'bottom',
sanitize: false,
content: function() {
$('.testDivCon').show()
$('.wifiInfoContent').append('<button >Click Me</button>')
return $('.wifiInfoContent').html();
}
});
});
$('.testDivCon').on('click', function() {
$('.testDiv').popover('hide');
$('.testDivCon').hide();
})
$('.testDiv').on('click',function() {
$('.testDivCon').hide()
});
$(document).on('click', '.testButton', function() {
console.log('clicked')
});
.testDivCon {
display: none;
position: absolute;
top: 0%;
left: 0%;
width: 100%;
height: 100%;
background-color: green;
}
.testDiv {
position: absolute;
top: 10%;
left: 20%;
width: 20%;
height: 15%;
background-color: black;
}
.wifiInfoContent {
display: none;
}
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj 3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4 1nzFpr53nxSS GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG 2QOK9T ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg OMhuP IlRH9sENBO0LRn5q 8nbTov4 1p" crossorigin="anonymous"></script>
<div ></div>
<div ></div>
<div ></div>
You don't need jQuery to achieve this and can perfectly do this in bootstrap 5 as well:
const popoverTrigger = document.querySelector('.testDiv');
const popover = new bootstrap.Popover(popoverTrigger, {
html: true,
sanitize: false,
content: '<button >Click Me</button>'
});
let testDivConShown = false;
popoverTrigger.addEventListener('click', function(){
testDivConShown = !testDivConShown;
document.querySelector('.testDivCon').style.display = testDivConShown ? 'block' : 'none';
});
document.querySelector('.testDivCon').addEventListener('click', function() {
popover.hide();
testDivConShown = false;
document.querySelector('.testDivCon').style.display = 'none';
});
document.body.addEventListener('click', function(e) {
if(e.target.classList.contains('testButton'))
console.log('button clicked');
});
.testDivCon {
display: none;
position: absolute;
top: 0%;
left: 0%;
width: 100%;
height: 100%;
background-color: green;
}
.testDiv {
position: absolute;
top: 10%;
left: 20%;
width: 20%;
height: 15%;
background-color: black;
}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u LWgxfKTRIcfu0JTxR EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
<div ></div>
<div data-bs-container="body" data-bs-toggle="popover" data-bs-placement="bottom"></div>
<div id="wifiInfoContent" ><button >Click Me</button></div>
