Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

ilovecode
ilovecode
8,071 Points

Fetch won't fire on Bootstrap 4 modal

I have a contact form that when I submit a modal must pop up and show a message. Then when I click the "Close" button it must fire a fetch request to reload the page. The fetch request does work, I tested it inside the submit button and it works in there.

The modal pops up when I fire the submit button, but when I click on the close button of the modal the handleRes(url) does not work.

I don't know why, because when I insert the handleRes(url) inside the submit event listener then it works.

The modal:

<div id="myModal" class="modal" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
          <div class="modal-content">
            <div class="modal-header">
              <h5 class="modal-title">Modal title</h5>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>
            <div class="modal-body">
              <p>Modal body text goes here.</p>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-primary">Save changes</button>
              <button type="button" class="btn btn-secondary modal-close" data-dismiss="modal">Close</button>
            </div>
          </div>
        </div>
      </div>  

Javascript:

const url = '/contact';
const submitForm = document.querySelector('#submit-post');
const closeModal = document.querySelector('.modal-close');

function handleRes (url) {
    fetch(url, {
        method: 'GET'
    })
    .then((response) => {
            if(response.ok) {
            return response;
        }
        throw Error(response.statusText);
    })
    .then( response => console.log(response.status) )
    .catch( error => console.log('There was an error!') );
}

function displayMessage() {
    console.log('this will be a message etc');
}

submitForm.addEventListener('click', (e) => {
    $('#myModal').on('shown.bs.modal', function () {
        displayMessage();
    })

    console.log('form submitted');
});

closeModal.addEventListener('click', () => {
    console.log('the modal closes');
    handleRes(url);
});

The form:

 <form class="post-form" action="/contact" method="post">
            <div class="form-group">
                <label for="firstName">First Name</label>
                <input type="text" name="firstName" class="form-control" id="firstName" placeholder="First Name">
            </div>
            <div class="form-group">
                <label for="lastName">Last Name</label>
                <input type="text" name="lastName" class="form-control" id="lastName" placeholder="Last Name">
            </div>
            <div class="form-group">
                <label for="email">Email</label>
                <input name="email" type="email" class="form-control" id="email" placeholder="Enter Name of Owl">
            </div>
            <div class="form-group">
                <label for="theQuery">What is your query?</label>
                <textarea name="theQuery" class="form-control" id="theQuery" rows="3"></textarea>
            </div>
            <button id="submit-post" type="submit" class="btn btn-outline-secondary mb-5 btn-do-magic" data-toggle="modal" data-target="#myModal">Send Owl</button>
        </form>