I have a button that toggles a bootstrap modal.
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
I want to do the same thing with a jquery function. Is this possible?
Just add data-toggle="collapse" and a data-target to element to automatically assign control of a collapsible element. The data-target attribute accepts a CSS selector to apply the collapse to. Be sure to add the class collapse to the collapsible element. If you'd like it to default open, add the additional class in.
jQuery css() Method The css() method sets or returns one or more style properties for the selected elements. When used to return properties: This method returns the specified CSS property value of the FIRST matched element.
give your button id and hide it:
CSS:
#btnTrigger
{
display:none;
}
HTML:
<button class="btn btn-primary btn-lg" id="btnTrigger" data-toggle="modal" data-target="#myModal">
and on someother event or condition just click it programmatically:
JQuery:
$('#btnTrigger').click();
In Result, popup will be displayed.
With simple code like you posted:
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
It won't show modal dialog unless you write code in javascript like:
$("button").on("click", function(){
$($(this).attr("data-target")).modal("show");
});
And what you will put in data-target
will open a div with id as dialog modal.
or
$("button").on("click", function(){
$($(this).data("target")).modal("show");
});
below code worked for me, you can call this function on any event
<script type="text/javascript">
$(document).ready(function () {
ShowModel = function () {
$("#myModal").modal();
}
});
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With