<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<title>JS Bin</title>
</head>
<body>
<h3>Bootstrap tooltips & popovers, and the <code>selector</code> option</h3>
<p>This JS Bin demonstrates the use of the <code>selector</code> option passed to the Bootstrap tooltip and popover JavaScript plugins.</p>
<p>The <code>selector</code> option essentially allows you to run tooltips and popovers using jQuery's <code>on</code> function, which means that you can allow dynamically added HTML with the correct selectors to trigger popups as if they were present in the originally loaded DOM. Without the selector option, only elements present in the initial DOM will trigger tooltips; any that are dynamically added will not.</p>
<p>Toggle the checkbox below and click the 'add new popover' button to observe the behavioral differences between using the selector option, and not using it.</p>
<hr>
<div class="checkbox">
<label id="use-selector-label">
<input id="use-selector" type="checkbox">
<span data-title="you must re-run this JS Bin to change the selector option once you've started the demo">Use selector option</span>
</label>
</div>
<pre id="with-selector-code">
$('body').popover({
selector: '.has-popover'
});
</pre>
<pre id="without-selector-code">
$('.has-popover').popover();
</pre>
<button class="btn btn-primary" id="add-button">Add new popover</button>
<div class="buttons">
<hr>
<button class="btn btn-block btn-success has-popover" data-title="Static" data-content="This button was specified in the initial HTML document" data-placement="top">Working popover</button>
</div>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/latest/js/bootstrap.js"></script>
</body>
</html>
body {
padding: 30px;
}
h3 {
line-height: 1.3;
}
.buttons,
pre {
display: none;
}
.btn-block {
margin-bottom: 5px;
}
function usingSelectorOption() {
return $('#use-selector').is(':checked');
}
function updateCodeView() {
$('#with-selector-code').toggle(usingSelectorOption());
$('#without-selector-code').toggle(!usingSelectorOption());
}
$(function() {
// Update code view when checkbox is toggled
updateCodeView();
$('#use-selector').click(function() {
updateCodeView();
});
var startedDemo = false;
$('#add-button').click(function() {
// One-time initialization
if (!startedDemo) {
if (usingSelectorOption()) {
$('body').popover({
selector: '.has-popover'
});
} else {
$('.has-popover').popover();
}
startedDemo = true;
}
// Disable selector checkbox, put a tooltip on it, and show the buttons panel
$('#use-selector').attr('disabled', 'disabled');
$('#use-selector-label span').tooltip();
$('.buttons').show();
// Add a new button that triggers (or doesn't) a popover, with the appropriate message
var button = null;
if (usingSelectorOption()) {
button = $('<button class="btn btn-block btn-success has-popover" data-title="Dynamic" data-content="This button was added dynamically by JavaScript" data-placement="top">Working dynamically added button</button>');
} else {
button = $('<button class="btn btn-block btn-default has-popover" data-title="Dynamic" data-content="This button was added dynamically by JavaScript" data-placement="top">Non-working dynamically added button</button>');
}
button.appendTo('.buttons');
});
});
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |