javascript - How to show/hide a bootstrap spinner with jQuery?

Javascript - How to show/hide a bootstrap spinner with jQuery?

To show and hide a Bootstrap spinner using jQuery, you can toggle its visibility by manipulating its CSS classes. Bootstrap provides a spinner component that you can control using classes like d-none (to hide) and spinner-border (to show). Here's how you can achieve this:

Example: Show/Hide Bootstrap Spinner with jQuery

Assume you have a button that triggers an action, and you want to show a spinner during the action:

HTML Structure

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Show/Hide Bootstrap Spinner</title> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <!-- jQuery --> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <style> .spinner-border { margin-top: 10px; /* Adjust as needed */ } </style> </head> <body> <div class="container mt-5"> <button id="actionButton" class="btn btn-primary">Start Action</button> <div id="spinner" class="spinner-border text-primary d-none" role="status"> <span class="sr-only">Loading...</span> </div> </div> <!-- Bootstrap JS --> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> <!-- Your custom script --> <script> $(document).ready(function() { $('#actionButton').click(function() { // Show spinner $('#spinner').removeClass('d-none'); // Simulate some action (e.g., API call, timeout) setTimeout(function() { // Hide spinner after some delay (simulated action completion) $('#spinner').addClass('d-none'); }, 3000); // Adjust timeout as needed }); }); </script> </body> </html> 

Explanation:

  1. HTML Structure:

    • The <button> with ID actionButton triggers an action.
    • The <div> with ID spinner contains the Bootstrap spinner (spinner-border) and starts hidden (d-none class).
  2. CSS:

    • Added minimal CSS to adjust the spinner's position (margin-top) as needed.
  3. JavaScript / jQuery:

    • Click Event: $('#actionButton').click(...) attaches a click event handler to the button.
    • Show Spinner: $('#spinner').removeClass('d-none') removes the d-none class to show the spinner.
    • Hide Spinner: Inside setTimeout, $('#spinner').addClass('d-none') adds the d-none class after a simulated delay (3 seconds in this example).
  4. Bootstrap Integration:

    • Include Bootstrap CSS (bootstrap.min.css) and Bootstrap JavaScript (bootstrap.min.js) for styling and functionality.

Notes:

  • Adjust the setTimeout delay (currently 3000 milliseconds) based on your actual action's duration.
  • Customize the spinner's appearance and position using additional Bootstrap or custom CSS styles.
  • Ensure jQuery (jquery.min.js) and Bootstrap (bootstrap.min.css and bootstrap.min.js) are correctly included and loaded.

By following these steps, you can effectively show and hide a Bootstrap spinner using jQuery in your web application whenever an action is triggered, providing visual feedback to users during loading or processing tasks.

Examples

  1. How to show a Bootstrap spinner using jQuery? Description: Display a Bootstrap spinner using jQuery by adding the d-block class to make it visible.

    $('#spinner').addClass('d-block'); 
  2. JavaScript code to hide a Bootstrap spinner with jQuery? Description: Hide a Bootstrap spinner using jQuery by removing the d-block class to make it invisible.

    $('#spinner').removeClass('d-block'); 
  3. How to toggle visibility of a Bootstrap spinner using jQuery? Description: Toggle the visibility of a Bootstrap spinner using jQuery based on its current state.

    $('#spinner').toggleClass('d-block'); 
  4. Show Bootstrap spinner while waiting for AJAX response in jQuery? Description: Display a Bootstrap spinner while waiting for an AJAX request to complete using jQuery.

    $(document).ajaxStart(function() { $('#spinner').addClass('d-block'); }).ajaxStop(function() { $('#spinner').removeClass('d-block'); }); 
  5. How to dynamically show/hide a Bootstrap spinner on button click with jQuery? Description: Toggle the visibility of a Bootstrap spinner based on a button click event using jQuery.

    <button id="toggleButton">Toggle Spinner</button> <div id="spinner" class="spinner-border d-none"></div> <script> $('#toggleButton').click(function() { $('#spinner').toggleClass('d-block'); }); </script> 
  6. JavaScript function to show Bootstrap spinner for form submission using jQuery? Description: Display a Bootstrap spinner during form submission using jQuery.

    <form id="myForm"> <button type="submit">Submit</button> </form> <div id="spinner" class="spinner-border d-none"></div> <script> $('#myForm').submit(function(event) { $('#spinner').addClass('d-block'); // Perform form submission logic here }); </script> 
  7. How to conditionally show/hide a Bootstrap spinner based on data loading status with jQuery? Description: Control the visibility of a Bootstrap spinner based on data loading conditions using jQuery.

    function fetchData() { $('#spinner').addClass('d-block'); // Perform data fetching logic here setTimeout(function() { $('#spinner').removeClass('d-block'); // Process data after loading }, 2000); // Example delay } 
  8. jQuery code to display Bootstrap spinner on page load until content is fully loaded? Description: Show a Bootstrap spinner on page load and hide it once all content is fully loaded using jQuery.

    $(window).on('load', function() { $('#spinner').removeClass('d-block'); }); 
  9. How to integrate Bootstrap spinner with jQuery AJAX beforeSend callback? Description: Utilize the beforeSend callback in jQuery AJAX to show a Bootstrap spinner before sending a request.

    $.ajax({ url: '/api/data', beforeSend: function() { $('#spinner').addClass('d-block'); }, success: function(data) { // Process data after successful AJAX call }, complete: function() { $('#spinner').removeClass('d-block'); } }); 
  10. JavaScript function to show/hide Bootstrap spinner based on user interaction with jQuery? Description: Toggle visibility of a Bootstrap spinner based on user interaction events using jQuery.

    $('#toggleButton').click(function() { $('#spinner').toggleClass('d-block'); }); 

More Tags

patch number-systems command-line-arguments cookiestore coap itunes android-threading python-2.x comparison fs-extra

More Programming Questions

More Livestock Calculators

More Mortgage and Real Estate Calculators

More Fitness Calculators

More Statistics Calculators