Trying to on submit/button generate loading (animation), bootstrap/js. Kicking my … , any assist would be appreciated. Assume the following, yes i have tried many variations, the JS is ‘calling an api for data’ on user submit, the data is rendered in a container call namesContainer, it all works [except] the loading, while rendering. Any assist would be great, an actual good link/bootstrap or other.
<button class="btn btn-primary" type="submit" id="generateNamesButton">
<span id="generateNamesText">Generate Names</span>
<span id="generateNamesSpinner" style="display: none">
<span class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true"></span>
<span class="visually-hidden">Loading...</span>
</span>
</button>
document.querySelector("form").addEventListener("submit", async function(event) {
event.preventDefault();
const inputIndustry = document.querySelector("#inputIndustry").value;
const languageSelect = document.querySelector("#languageSelect");
const selectedLanguage = languageSelect.options[languageSelect.selectedIndex].value;
showLoader(true);
try {
const names = await generateNames(inputIndustry, selectedLanguage);
const namesContainer = document.querySelector("#namesContainer");
const card = namesContainer.parentElement;
card.style.display = "block";
namesContainer.innerHTML = `<ul>${names.split("n").map(name => `<li>${name}</li>`).join("")}</ul>`;
} catch (error) {
console.error(error);
const namesContainer = document.querySelector("#namesContainer");
namesContainer.innerHTML = "Error generating names";
}
showLoader(false);
});
function showLoader(show) {
const button = document.querySelector("#generateNamesButton");
if (show) {
button.disabled = true;
button.innerHTML = `<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
<span class="visually-hidden">Loading...</span>`;
} else {
button.disabled = false;
button.innerHTML = `<span id="generateNamesText">Generate Names</span>
<span id="generateNamesSpinner" style="display: none;">
<span class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true"></span>
<span class="visually-hidden">Loading...</span>
</span>`;
}
}
function showLoader(show) {
const button = document.querySelector("#generateNamesButton");
if (show) {
button.disabled = true;
button.innerHTML = `<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>
<span class="visually-hidden">Loading...</span>`;
} else {
button.disabled = false;
button.innerHTML = `<span id="generateNamesText">Generate Names</span>
<span id="generateNamesSpinner" style="display: none;">
<span class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true"></span>
<span class="visually-hidden">Loading...</span>
</span>`;
}
}
[button=submit], [loading/visible], results rendered [loading/hidden]..
2
Answers
So basically what you mean is that you have a button that when clicked submits some data to the form element. While the onSubmit() is being executed, you need to display a "Loading…" animation on your submit button
To achieve this, follow this approach:
Here, toggleLoading() will set loading to true if it’s false and false if loading variable is true. You can replace the Loading… text with the bootstrap loading animation code
But please note that changes will be clearly visible only if the processing task is an asynchronous task requiring over 1-2 seconds of time