Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perform data-toggle with jquery

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?

like image 977
Lord Vermillion Avatar asked Apr 23 '14 07:04

Lord Vermillion


People also ask

How do you toggle data in JavaScript?

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.

What is the use of CSS () method in jQuery?

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.


3 Answers

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.

like image 175
Ehsan Sajjad Avatar answered Oct 14 '22 19:10

Ehsan Sajjad


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");
});
like image 35
Snake Eyes Avatar answered Oct 14 '22 18:10

Snake Eyes


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>
like image 23
sridharnetha Avatar answered Oct 14 '22 18:10

sridharnetha