Title: Programmatically open a modal
Last modified: January 3, 2024

---

# Programmatically open a modal

 *  Resolved [piersrr](https://wordpress.org/support/users/piersrr/)
 * (@piersrr)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/programmatically-open-a-modal/)
 * Thank you so much for the plugin. It’s been a real help.
 * I am looking to open the modal under certain conditions, in my case triggering
   it from Javascript in the footer.
 * I’ve found bodModal.initModal(); only opens the modal on the page when the modal
   has already been open (e.g. on page load).
 * Is there a way I can call to open a modal through code?
 * Thanks

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Plugin Author [merbmerb](https://wordpress.org/support/users/merbmerb/)
 * (@merbmerb)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/programmatically-open-a-modal/#post-17316055)
 * Easiest way to do this is to define the modal so it triggers on a class, you 
   can then trigger the modal by triggering a click on the class via your code.
 *  Thread Starter [piersrr](https://wordpress.org/support/users/piersrr/)
 * (@piersrr)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/programmatically-open-a-modal/#post-17316375)
 * Thanks. I’ve added something simple to trigger a click using document.addEventListener(‘
   DOMContentLoaded’, etc but the modal doesn’t appear?
 * The site [is here](https://agri.piers.cc/basket/) and I have added the click 
   event to the copyright bar at the bottom of the page
 *  Plugin Author [merbmerb](https://wordpress.org/support/users/merbmerb/)
 * (@merbmerb)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/programmatically-open-a-modal/#post-17316440)
 * Hi
 * I could not find your code to trigger the click, but I did my own test. I created
   a modal using a custom element click on the class ‘launch’. I then used the following
   code to test
 *     ```wp-block-code
       <p class="triggertest" >Lets do a simple trigger test</p>
       <script>
       document.querySelector('.triggertest').onclick = function() {
       jQuery(".launch").trigger("click");
       };
   
       </script>
       ```
   
 * The above code detects a click in the text ‘Lets do a simple trigger test’ and
   then launches the modal by triggering a click on the class ‘.launch’. Sorry not
   sure what the JS is but worked without issue using jQuery. Obviously you would
   just need to use the trigger line where you need it.
 *  Thread Starter [piersrr](https://wordpress.org/support/users/piersrr/)
 * (@piersrr)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/programmatically-open-a-modal/#post-17318880)
 * Ah sorry. Thanks for the pointer. I was using document.addEventListener(‘DOMContentLoaded’,
   function (){//clicker} which wasn’t working, but it worked with: window.onload
   = function() {//clicker}
   It’s now working! Thanks
    -  This reply was modified 2 years, 4 months ago by [piersrr](https://wordpress.org/support/users/piersrr/).

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Programmatically open a modal’ is closed to new replies.

 * ![](https://ps.w.org/modal-block/assets/icon-256x256.png?rev=2149842)
 * [Modal Guten Block](https://wordpress.org/plugins/modal-block/)
 * [Support Threads](https://wordpress.org/support/plugin/modal-block/)
 * [Active Topics](https://wordpress.org/support/plugin/modal-block/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/modal-block/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/modal-block/reviews/)

 * 4 replies
 * 2 participants
 * Last reply from: [piersrr](https://wordpress.org/support/users/piersrr/)
 * Last activity: [2 years, 4 months ago](https://wordpress.org/support/topic/programmatically-open-a-modal/#post-17318880)
 * Status: resolved