Pressing a button will display a div element, which will automatically be hidden after 5 seconds

I'm working on an email submission form that displays a confirmation message below the input field when a user submits their email address. The confirmation text should fade out after 5 seconds.

Here's the code snippet:

<div class="input-group newsletter-group">
    <input type="text" class="form-control" id="email-to-submit">
    <div id="submit-email" class="input-group-addon">go!</div>
</div>
<div id="email-submit-form">THANKS!</div>
<div id="invalid-email-warning" style="color: red; display: none;">Not an email address</div>

$(function() {
   setTimeout(function() { $("#email-submit-form").fadeOut(1500); }, 5000)
        $('#submit-email').click(function() {
            var emailAddress = $('#email-to-submit').val();
                if (validateEmail(emailAddress)){
                    $('#email-to-submit').val('');
                    $('#email-submit-form');
                    $.ajax({
                        type: "POST",
                        url: '/submitEmailAddress',
                        dataType: 'json',
                        data: JSON.stringify({'email': emailAddress})
                    });
                } else {
                    $('#invalid-email-warning').show();
                  }
            $('#email-submit-form').show();
            setTimeout(function() { $("#email-submit-form").fadeOut(1500); }, 5000)
    })
});

Answer №1

One possible solution is to utilize jQuery's delay() function.

$('#email-submit-form').fadeIn().delay(5000).fadeOut();

Answer №2

Here's a suggestion for you:

 $('#email-submit-form').show('slow', function () {
        $(this).delay(5000).hide('slow');
      });

Answer №3

Include your timeout logic within the if(){ statement or in the ajax success callback

$.ajax({
    type: "POST",
    url: '/submitEmailAddress',
    dataType: 'json',
    data: JSON.stringify({
        'email': emailAddress
    }),
    success: function (response) {
        $('#email-submit-form').show();
        setTimeout(function () {
            $("#email-submit-form").fadeOut(1500);
        }, 5000)
    }
});

Additionally,

if (validateEmail(emailAddress)){
    $('#email-to-submit').val('');
    // Remove the following line: $('#email-submit-form');

The specified line serves no purpose, so it can be deleted.

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

Conceal flexbox item depending on neighboring element dimensions or content

I've encountered an issue while using a flexbox to display two elements side by side. The left element contains text, while the right one holds a number. When the value in the right element has at least 7 digits ( > 999,999 or < -999,999), I ne ...

What is the best way to adjust the layout for smaller screens using material ui?

Just starting out with responsive material design. On smaller screens, the Sports Status needs to remain in the right corner. 4Standard, 5Standard, 6Standard, 7Standard, 8Standard, 9Standard should wrap to the next line. I came across this code snippet f ...

When the Materialparameters property transparent is set to true in threejs, the object becomes invisible

Currently, I am enrolled in Bruno Simons' class and in the session "Haunted House," he is teaching how to incorporate textures. So far, everything seems to be running smoothly and all the map textures are functioning perfectly. However, when I input t ...

Save the environment value within Vue when the app starts up

When working with a basic web app created using VueJS, the application makes an API call that returns an object containing the environment name. For instance: https://appsdev/mysimpleapp/api/environment returns {"applicationName":"My S ...

Using Threejs JSONLoader and OOP to easily add models to a scene post-loading

I am working on incorporating the THREE JSONLoader into a "Scenemanager" Object that manages the addition and removal of objects and models. My main goal is to deepen my understanding of OOP, JS, and Threejs. Within App3D (which oversees the scene), I cal ...

Enhance the material-table component by incorporating dynamic information into the lookup property

I am currently facing challenges while attempting to dynamically add data to the lookup property in the Material-Table component. The lookup is structured as an object, and you can refer to its definition in the initial example provided here. However, it ...

Using JavaScript import may encounter issues when trying to access the same file or a different file

When importing something and using it, there are certain scenarios where it may not work as expected. For example: <html> <body> <button onclick="foo()">Click Me</button> </body> <script type="module"> ...

Prevent users from entering past dates in the date input field

In my project, I have decided to use input type date instead of datepicker. Is there a way to prevent users from selecting back dates in input type date without using datepicker? If you have any suggestions, please let me know. Thank you! ...

Having trouble replacing scss variables in react-h5-audio-player

I recently integrated react-h5-audio-player into my project and followed the instructions on the README page to customize the styles by updating the SCSS variables that control the colors. However, it appears that my custom styles are not being applied. An ...

The impact of array splicing on data manipulation

I have a $scope array variable that I'm using to generate tabs on the front end, but I'm struggling with implementing a remove tab function. The code for removing tabs is as follows: function removeTab(index) { $scope.tabs.splice(index, 1); ...

In AngularJS, modifying the value of a copied variable will result in the corresponding change in the value of the main

I'm facing a peculiar issue. I have an array of objects and I used angular.forEach to update the price key value of each object. However, when I make changes in each object, it also affects the main array object. Take a look at the code snippet below ...

How to highlight all the text within a 'pre code block' when double-clicked using JavaScript

Is there a way to make code blocks on my blog automatically selected when double-clicked without using jQuery? Here is the code I have so far: I apologize if this is a silly question, I am still learning! <script type="text/javascript" src="https://c ...

Saving Data in an Angular Material Table: A How-to Guide

I have a basic angular material table and I am looking for a way to save the data displayed in each row when a button is clicked. Is it possible to save each row of data as an object and push it to an array? If so, how can I achieve this? <div class=& ...

Concealing the view-source feature in core PHP or HTML

Is there a way to conceal view-source in basic php or html? I've come across some information on this topic, but I'm having trouble grasping how to implement it: Web Developer Class: How to Hide your Source Code ...

Embedding JSON data in a GSP page

My goal is to transfer JSON data to a GSP page and present it in a table format. The expected JSON structure: { "data": [ [ "Tiger Nixon", "System Architect", "Edinburgh" ] ]} I attempted to achieve this with the following co ...

Efficient Techniques for Concealing and Revealing Navigation Menus using HTML, CSS, and JavaScript

I need help making the menu icon toggle the navigate menu between showing and hiding when clicked on the top right corner of the screen. For better understanding, here is an accompanying image: https://i.stack.imgur.com/oy6d9.png However, the current co ...

divide a JSON list

Within this JSON array, there is a plethora of person data, each with their own specified type such as "hr" or "accounting". The structure of the JSON array is depicted below: { "0": { "id": "2", "name": "blabla", "type": "hr" ...

Infinite layers of options in the dropdown navigation bar

I am facing an issue with a dynamically generated unordered list that I want to turn into a dropdown menu. The challenge is that I do not know how many levels there will be, and I only want to display one level at a time. Here is what I have tried so far ...

Decoding JavaScript elements embedded in an HTML website

My current HTML site includes the following code: <script type="text/javascript"> jwplayer('player_container').setup( { 'width': '640', ...

What are the best practices for securely storing a distinctive client identifier for websockets?

In order to differentiate and identify the specific client sending a request through web-sockets, I require a unique client identifier. However, I'm uncertain about the optimal method for storing this identifier so that it can be included with every s ...