Is it possible to have Stimulus actions triggered inside Bootstrap popovers’ elements?
In the example below (html + Stimulus contoller), clicking on a button prints ‘FOO’ in the console, except when this button appears in a popup.
I understand showing the popover dynamically changes the DOM (see ‘content: function…’ below).
If it’s not possible, how can Stimulus code be called from plain javascript:
.. onclick="$('#controller_div').<trigger the "foo" action on the associated controller ???>" ..
import {Controller} from 'stimulus'
export default class extends Controller {
foo() {console.log('FOO')}
}
<h3>Bootstrap 4 Popover & Stimulus</h3>
<p>Check the JS console for messages ("FOO")</p>
<div id="sandbox">
<div id="controller_div" data-controller="foobar">
<a data-action="foobar#foo" id="example_1" data-toggle="popover" href="">Show the popover</a>
<br>
<button data-action="foobar#foo">Some clickable button (SUCCESS)</button>
<div id="popover-content-example_1" class="d-none">
<button data-action="foobar#foo">Some clickable button (FAIL)</button>
</div>
</div>
</div>
<script>
document.addEventListener("turbolinks:load", function () {
$("[data-toggle='popover']").each(function () {
$(this).popover({
html: true,
trigger: 'focus',
content: function () {
var id = $(this).attr('id')
return $('#popover-content-' + id).html();
}
});
}).click(function (e) {
e.preventDefault();
}).mouseenter(function (e) {
// $(this).popover('show');
});
})
</script>