I'm pretty new to Ajax calls and promises, and I'm trying to load a form in a modal once the user clicks the sign up button on my app. To do that, I dynamically load the form with an Ajax call using the fetch() API. The fetch API calls the endpoint /accounts/signup which renders my HTML form:
header.html
<body>
<div class="container">
<header class="header">
<!-- modal window -->
<div class="modal hidden">
<button class="close-modal">×</button>
<p class="modal-text">
</p>
</div>
<!-- End of modal window -->
/accounts/signup
<h2>Sign up by filling in the form:</h2>
<form method="POST" id="signup-form">
{% csrf_token %}
{% for field in form %}
<p>
{{ field.label_tag }}<br>
{{ field }}
{% if field.help_text %}
<small style="color: grey">{{ field.help_text }}</small>
{% endif %}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
</p>
{% endfor %}
<button type="submit" id="signup-submit">Sign up</button>
</form>
Ajax call:
const getForm = function(endpoint) {
const request = fetch(endpoint)
.then(response => response.text())
.then(data => modal.innerHTML = data)
}
btnSignup.addEventListener('click', function(){
getForm('/accounts/signup')
document.getElementById('signup-form').addEventListener('load', function(e){
e.preventDefault()
})
})
As you can see I'm successfully loading the HTML form on the modal using promises. The issue I have is that after rendering the form I want to handle it adding preventDefault() and sending it with another Ajax call. When I try to select the form and add i.e. preventDefault(), I am not able to do that.
The error I get is:
Uncaught TypeError: Cannot read property 'addEventListener' of null
at HTMLAnchorElement.<anonymous>
Which is basically JS not finding the id="signup-form" which was dynamically loaded on the page. I get the same error if I try to chain another .then() method and perform the task there:
const getForm = function(endpoint) {
const request = fetch(endpoint)
.then(response => response.text())
.then(data => modal.innerHTML = data)
.then(console.log(document.getElementById('signup-form')))
}
btnSignup.addEventListener('click', function(){getForm('/accounts/signup')})
How would you usually handle such use cases? What is the best practice when it comes to handling dynamically loaded HTML forms with Ajax and promises?