John Main Logo

John Main

Code. Design. Hosting. Maintenance.

Mar '19

Simple Popover Script

I spent some time messing about with Bootstrap popovers, and other similar tools but was finding that they tended to be overly complicated to use and skin, and also mostly wouldn’t allow form elements to be inside them. I found myself with a task where I was very tight on space and needed to add several textareas to my form. A popover was the obvious choice, but many relied on convoluted attributes on the trigger element or involved javascript handlers. Even more importantly most tend to destroy the popover when it is closed, which is clearly no use I want a form element in there.

After a bit of playing around I came up with this script myself:


.popover-container {
position: relative;
.popover-content {
z-index: 10000;
border: 1px solid #555555;
padding: 8px;
margin-top: 10px;
width: 300px;
position: absolute;
left: -1000000px;
} {
left: -150px;
} {
content: '';
display: block;
position: absolute;
top: -6px;
left: 150px;
width: 10px;
height: 10px;
background: #FFFFFF;
border-right:1px solid #555555;
border-top:1px solid #555555;


$().ready(function() {
$('.popover-container').on('click', 'button', function() {
var parent = $(this).parents('.popover-container');
var content = parent.find('.popover-content');
if(content.hasClass('open')) {
else {
//Clear down any others first
$('').each(function() {
var offset = 150 - (parent.width() / 2) - 2;
content.css('left', '-' + offset + 'px');
//Close on blur
$('.popover-container').on('blur', '.popover-content', function() {
//Close on escape key
$(document).keyup(function(e) {
if (e.keyCode === 27) {
$('').each(function() {
function closePopover(element) {
element.css('left', '');

Example HTML:

<div class="popover-container">
<button type="button" class="btn btn-xs btn-default">
Show Popover
<div class="popover-content">
<div class="form-group">
<p>This is my popover</p>
<textarea name="my-textarea"></textarea>

Note that the CSS and JS above are based around the popover being 300px wide. You can change this to whatever you like, but bear in mind that the centering (the 150px offsets) needs to be adjusted in a corresponding way.

Leave a Reply