Swap out the div element with a loading.gif image right before the page finishes loading, then pause for 3 seconds before getting rid of

My challenge involves a select drop down that is being dynamically built. To handle this, I have implemented a setTimeout function to change the selected option after a 3-second delay. This allows the select element to load first before I manipulate it. However, I would like to enhance this functionality by hiding the select element initially, displaying a loading gif, and then revealing the select element with the forced option after the 3 seconds have elapsed.

If you'd like to see my progress so far, feel free to check out this JSFiddle: https://jsfiddle.net/88ot9vya/1/

Currently, I am using the following setTimeout function to change the option successfully:

setTimeout(function () {
    $('#txtShipAddress>option:eq(0)').prop('selected', true);
}, 3000);

My question now is, how can I incorporate a loading gif before initiating the setTimeout function, then reveal the select element while hiding the loading gif after the delay? Feel free to use any loading gif example, such as this one:

Answer №1

I inserted an image tag and initially concealed the selection.

https://jsfiddle.net/88ot9vya/2/

The gif does not load in the fiddle, but it displays the image followed by the selection.

<select name="txtShipAddress" id="txtShipAddress" style="display:none;">
    <option value="-2">Please Choose One</option>
    <option value="115599">#1</option>    
    <option value="141044">#2</option>    
    <option value="143936">#3</option>    
    <option value="143414">#4</option>    
    <option value="-1" selected>Free Form Address</option>
</select>
<img id="giffy-poo"  src="https://giphy.com/gifs/loading-TkXCbTx9gfUJi?utm_source=media-link&utm_medium=landing&utm_campaign=Media%20Links&utm_term=https://www.google.com/" />

setTimeout(function () {
    $('#txtShipAddress>option:eq(0)').prop('selected', true);
    $('#giffy-poo').hide()
    $('select').show()
}, 3000);

Answer №2

I followed a similar structure to @Kosch

In my solution, I utilized a document ready event handler to ensure that the select element is rendered before executing the script

$(document).ready(function(){
$('#txtShipAddress').hide();
  setTimeout(function () {
      $('#txtShipAddress>option:eq(0)').prop('selected', true);
      $('#txtShipAddress').show();
      $('#loader').hide();
  }, 3000);

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="txtShipAddress" id="txtShipAddress" >
    <option value="-2">Please Choose One</option>
    <option value="115599">#1</option>    
    <option value="141044">#2</option>    
    <option value="143936">#3</option>    
    <option value="143414">#4</option>    
    <option value="-1" selected>Free Form Address</option>
</select>
<img id="loader"  src="https://media.giphy.com/media/nZQIwSpCXFweQ/giphy.gif" />

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieving the previous value using JQuery's onChange event

Is there a way to retrieve the initial quantity value before it is changed with the onChange function in this code snippet? I'm encountering issues with the CSS while using this setup for an input box. You can view the problematic CSS here. Updating ...

Scaling CSS to fit both height and width (Stretch)

My content is contained within an 800x480 pixel box. When the window size changes, I want to achieve the following: Expand/Stretch the container to fill the entire screen Include all elements inside the container and maintain their relative positions I a ...

Looking to utilize AngularJS validation in place of the default browser validation?

I am working on a form that contains two ng-forms for input validation. I have encountered two issues with my forms. 1) I am trying to implement a minlength validation for the Company input, but my current approach doesn't seem to be working. How can ...

Unable to detect hover (etc) events after generating div elements with innerHTML method

After using the code below to generate some divs document.getElementById('container').innerHTML += '<div class="colorBox" id="box'+i+'"></div>'; I am encountering an issue with capturing hover events: $(".colorB ...

What is the best way to display the print using AJAX and jQuery?

Currently, I'm diving into the world of AJAX and jQuery. At the moment, when I execute this PHP script on its own, I have to sit back and wait while the loading wheel spins until the loop finishes. while ( $row = mysql_fetch_array($res) ) { postc ...

Animation showing on the progress bar is not correctly halted

I am encountering a problem with handling an animation of a progress bar in my code. The issue arises when I pause the animation, as it seems to slightly backtrack instead of pausing at the correct position upon clicking a button. Can someone help identify ...

What is the best way to invert the positioning of the li elements to move upwards?

https://i.stack.imgur.com/mZaoS.png Seeking assistance on adjusting the height of bars to start from the bottom and go upwards instead of starting from the top position and going downwards. The JavaScript code below is used to generate the li elements and ...

Pagination for comments using ajax in Wordpress

I am currently fetching my Wordpress comments from the database through a custom query and displaying them accordingly. I am utilizing the paginate_links() function in Wordpress to paginate the comments. Below is the code snippet: <div class="commentsW ...

Contrasting WebSQL and SQLite in terms of their utility in mobile applications and web browsers

Could you confirm if WebSQL and SQLite are the same? Are both WebSQL and SQLite available in PhoneGap? Will the JavaScript code used for WebSQL in a web browser be the same for a mobile app, or will we need different code? What advantages does WebSQL ha ...

Adding or Deleting Rows from Input Table

I'm in the process of creating a website that features an input table allowing users to easily add or remove rows at their discretion. The desired UI is shown below: https://i.sstatic.net/EFAlM.jpg Here is the HTML code I have developed so far: & ...

Analyze two sets of JSON data and compile a new JSON containing only the shared values

I am trying to generate two JSON arrays based on a shared property value from comparing two sets of JSON data. this.linkedParticipants =[ { "id": 3, "name": "Participant 2", "email": "<a href="/ ...

Receiving RSVP via email after sending out invitations

My dilemma lies in sending an invitation message via email to two individuals, complete with two link buttons offering the options to either Approve or Reject. The question now becomes: How can I track who has clicked on the Approve button? This way, I ca ...

Creating a flexible Tic Tac Toe board using only HTML and CSS without any JavaScript

I'm currently tackling an HTML layout challenge that involves creating a responsive Tic Tac Toe board using only HTML and CSS, without any JavaScript. This is how I have structured the layout of the board: <div class="board"> <div class= ...

Click to enlarge the text on button press

Recently, I've been working on a project involving a table that has a button at the end. My goal is to double the text size of the rows in the table when the button is clicked. As someone who is new to JQuery, this question may sound basic but any hel ...

Is it possible to create a list item animation using only one div element?

I'm currently working on achieving a dynamic animation effect for list items similar to the one demonstrated in Pasquale D'Silva's design example: https://medium.com/design-ux/926eb80d64e3#1f47 In this animation, the list item disappears wh ...

Ways to usually connect forms in angular

I created a template form based on various guides, but they are not working as expected. I have defined two models: export class User { constructor(public userId?: UserId, public firstName?: String, public lastName?: String, ...

"Triggering an event when a checkbox in a list is clicked

How can I add an onclick event to a checkbox within an <li> element, as shown in the picture? Here is my JavaScript code so far: It's not working. $(document).ready(function() { allFiles() /*----------------------------------------------- ...

Having trouble with the jQuery toggle functionality not working as expected

I'm implementing a function where a div should increase in height and opacity when clicked, and then revert back to its original state when clicked again. I used the toggle function for this purpose, but the issue is that the button disappears when th ...

Dynamically loading Jquery tabs in AngularJS can enhance user experience and

I need to implement a feature that loads tabs from an array using AngularJS. $scope.hods = [ { leader_id: 1000321, staff_id: 1000321, longname: "Ooi Kok Hong", username: "ANDREW", team_role: "HOD", importance: "1", active:true }, { leader_id: 100 ...

Align buttons in the center of a card or container using HTML and CSS

Hi everyone, this is my first time posting so please go easy on me. I've been struggling to align the buttons at the bottom of my cards to make them look neater. I've tried using flexbox but it doesn't seem to be working. Some people have su ...