Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a modal in javascript instead of manual click? [duplicate]

I have a modal which is open when i click on a button. Now i want to open the modal in a javascript function. Here is the working modal :

<a href="#" class="btn btn-lg btn-primary" id="sentMessage" data-toggle="modal" data-target="#largeModal">Click to open Modal</a>
    <div class="modal fade" id="largeModal" tabindex="-1" role="dialog" aria-labelledby="largeModal" aria-hidden="true">
      <div class="modal-dialog modal-lg">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title" id="myModalLabel">Large Modal</h4>
          </div>
          <div class="modal-body">
            <h3>Modal Body</h3>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>

I want to open the same modal in a javascript function. So to simulate this function, i create another button which trigger a function :

<a href="#" ng-click="openModal();" class="btn btn-lg btn-primary">Click to execute js</a>

Here is my openModal function :

$scope.openModal = function(){
    $("#sentMessage").click();

};

When i click on the button "Click to execute js", the modal does not open. Here is the plunker

like image 418
Pracede Avatar asked May 22 '15 12:05

Pracede


People also ask

How do I open a modal when I click a button?

To trigger the modal window, you need to use a button or a link. Then include the two data-* attributes: data-toggle="modal" opens the modal window. data-target="#myModal" points to the id of the modal.

How do you trigger a modal in JavaScript?

Trigger the Modal Via data-* Attributes Add data-toggle="modal" and data-target="#modalID" to any element.

How do I open modal pop on href onclick?

Just change the button for <a href="#" id="myBtn">Open Modal</a> , your current script will assign the onclick method.

How do you make a modal not close on click outside?

Data-keyword="false" is to prevent closing modal while clicking Esc button, while data-backdrop="static" , allows you keep modal pop-up opened when clicking on Gray area.


1 Answers

you can use the modal function:

$('#largeModal').modal('show');
like image 164
Mivaweb Avatar answered Oct 24 '22 00:10

Mivaweb