0

I have the following modal:

<div class="modal fade" id="m_modal_1" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">

I have the following button that pops up the modal:

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#m_modal_1"> Launch Modal</button>

Instead of initiating from the button, how do I popup from code? I was thinking something like this:

if(status == 1){

    modal("show");
}
1
  • You could give an id to your button, hide it and trigger click event like this $('#yourButton').click(); Commented Oct 5, 2019 at 1:06

4 Answers 4

2

Assuming that you're using Bootstrap (based on the classes).

You can use the following:

$('#m_modal_1').modal('show')
Sign up to request clarification or add additional context in comments.

Comments

0

You need to add eventlistener to any element, so once the event is triggered you change classes of the modal element or style.

Assuming you have some element (ie button with classname btn and modal with classname modal)

document.querySelector('.btn').addEventListener('click', () => {
  document.querySelector('.modal').classList.toggle('hidden');
}

And your css can be like this

.hidden {
  display: none;
  // or
  opacity: 0;
}

Comments

0

I think you are using bootstrap. so in your case

if(status == 1){

    $("#m_modal_1").modal("show");
}

"show" is a option of modal function. See more option and usage here https://getbootstrap.com/docs/4.0/components/modal/#options

Comments

0

Basically it depends on when you want to popup your modal.suppose you want to popup on page ready then,

$(document).ready(function(e){
$(#modal_id).show();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.