Displaying a div only when all input fields have been completed

I need help figuring out how to display a div after checking if all input fields are filled. Specifically, I want to only check the inputs within the #intro-info section. Can someone provide guidance on what to include in the else statement so that the button #intro-button will appear when all inputs are filled?

Here is a fiddle link for reference

$(function () {   
    var intro = $('.intro');

    intro.on('keypress', function() {
        if ($(this).val().length > 1) {
            $(this).next('.intro').addClass('block');
        }         
       /* else {
            $('.intro').hide();
        }*/
    });
    var allEmpty = true;

    $('#intro-info :input').each(function() {
        if ($(this).val() !== '') {
            allEmpty = false;
            return false; // stop iterating once a non-empty input is found
        } else
    });
     return allEmpty;
});
.intro {
  opacity: 0;
  padding: 10px 15px;
  margin: 20px auto;
}

.intro:first-child {
  display: block;
  opacity: 1;
}
.block {
  display: block;
  opacity: 1;
   -webkit-animation: fadein 1s ease-in;
   -moz-animation: fadein 1s ease-in;
    animation: fadein 1s ease-in;
}
.next {
  display: none;
}
#intro-button {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="intro-info">
  <input id="name" type="text" class="intro">
  <input id="email" type="email" class="intro">
  <input id="title" type="text" class="intro">
  <button id="intro-button">Proceed</button>
</div>



<a class="next">show div</a>

Answer №1

To control the visibility of an element, you can utilize jQuery's toggle() method. This involves gathering input from text fields, checking their lengths, and then invoking this function with the result.

Here is a sample implementation:

$('.show').toggle($('#email').val().length > 0);

This code snippet demonstrates how to toggle the visibility of the show div based on whether or not the email field contains any input.

Answer №2

  • Prefer using the input event over keypress
  • Utilize jQuery.filter to test all values within the form excluding button

It is important to remember that the selector for the next button should be .next, not #next

$(function() {
  var intro = $('.intro');
  var testFunction = function() {
    return $('#intro-info :input:not(button)').filter(function() {
      return this.value == '';
    })
  };
  intro.on('input', function() {
    $('.next').toggle(!testFunction().length);
  });
});
.intro {
  //opacity: 0;
  padding: 10px 15px;
  margin: 20px auto;
}
.intro:first-child {
  display: block;
  opacity: 1;
}
.block {
  display: block;
  opacity: 1;
  -webkit-animation: fadein 1s ease-in;
  -moz-animation: fadein 1s ease-in;
  animation: fadein 1s ease-in;
}
.next {
  display: none;
}
#intro-button {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="intro-info">
  <input id="name" type="text" class="intro">
  <input id="email" type="email" class="intro">
  <input id="title" type="text" class="intro">
  <button id="intro-button">Proceed</button>
</div>
<a class="next">show div</a>

Answer №3

  • Modify your keypress event listener to listen for input instead.

  • In CSS, the id selector takes precedence over the class selector. For example, #intro-button{ display:none} will override .block{display:block}. Therefore, even if the class block is added, the display will still be none.

    To solve this issue, you can use the !important declaration in the .block property for display:

    .block {
      display: block !important;
    }
    
  • JavaScript section : Add a class once all inputs are filled in.

    if (intro.filter(function(){
      return $(this).val().length > 1
    }).length === intro.length){
      $("#intro-button").addClass('block');
    }else {
      $("#intro-button").removeClass('block');
    }
    

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

Using a tuple as a key in a Map in Typescript/Javascript can be a powerful

Encountered a strange bug in my code where I'm struggling to achieve constant time lookup from a Map when using a tuple as the key. Here is an example highlighting the issue, along with the workaround I am currently using to make it function: hello. ...

Applying style changes to the height on scroll event in Javascript for Chrome, Firefox, and Internet Explorer

I am working on triggering an event when scrolling to a specific height. I have code that successfully adds a style in Chrome, but it is not functioning properly in IE. Can anyone provide assistance? myID = document.getElementById("subnav"); var mySc ...

Limit the maximum column gap on the grid and stop it from expanding further

I'm currently facing an issue with some columns/rows on my website that are keeping content locked in the top left corner, reminiscent of early 00's web design. The site was originally designed for smaller screens like 1024x764, so when viewed on ...

What is the method for transforming an array into an object with properties (JSON) in JavaScript or ES6?

Here is the array that I have: const array = [2,3]; I am looking to transform this array into an object like the following: "media" :[{"mid":"2"},{"mid":"3"}] Any help would be greatly appreciated. ...

The npm package for @azure/storage-blob doesn't seem to play nice with the Azure Blob storage modules for IoT Edge

Currently, I am developing an edge module using Node.js to interact with Azure Blob storage modules on IoT Edge platform. To achieve this, I am following the documentation available at https://www.npmjs.com/package/@azure/storage-blob. The npm package ut ...

What is the best way to keep a heading anchored to the bottom of an informative hover container when using a transform scale effect?

I'm trying to make the title stay at the bottom of the hover box when a user hovers over it. I want the hover box to appear with the title always at the bottom, and close upwards from the title as the description covers the entire box. How can I achie ...

What is the best way to add a border line underneath an image?

I have been struggling to find a solution for my problem, so I decided to post this topic in the hopes that someone can help me out. I am facing an issue with the image overlay border line in the "Service provided" section. Despite trying, I have not been ...

What is the process of handling the event queue in Node.js?

Exploring the intricacies of the Node.js architecture has left me with several questions: 1) Is the event loop a component of libuv or v8? 2) How does the callback in the event queue transition to the call stack for execution after being delegated by the ...

Error in Angular: IE8 is expecting an identifier

My application runs smoothly on most browsers, but I encounter an issue with IE8 where an Expected Identifier error occurs. $scope.delete = function (index) { $scope.recipelists.splice(index, 1); localStorage.setItem('markedRecipes ...

Styling background images with jQuery

Issue with Implementing jQuery Background Image Swapper and CSS Styling html { background: url(images/image_1.jpg) no-repeat center center fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; b ...

Trouble encountered while attempting to utilize @click="checkedInput" for displaying checkbox label in Vue.js?

const app = new Vue({ el: '#app', data: { checkedNames: [], checkedName: true, close: false }, methods: { uncheck(checkedName) { this.checkedNames = this.checkedNames.filter(name => name !== checkedName); }, ...

What could be causing the Angular router outlet to not route properly?

Check out this demo showcasing 2 outlets (Defined in app.module.ts): <router-outlet></router-outlet> <router-outlet name="b"></router-outlet> The specified routes are: const routes: Routes = [ { path: 'a', com ...

Show relevant details of a user by specifying their username following the /user/ URL

Is there a way to view a user's profile on Facebook by going to their specific URL? I've searched around but can't seem to find any information on how to do this. Can anyone provide some guidance or assistance? ...

Tips for sending an API request from a local Ionic server to a separate local Express server

I currently have two applications running on my local machine: an ionic2 app running on http://localhost:8041 and an express app running on http://localhost:8000. Using Angular 2 observables, I am encountering an issue when making API calls with absolute p ...

Populate Vue 3 Element-plus Virtualized Table with actual data

As a newcomer to frontend development, I am currently working on integrating Element-plus Virtualized Table with actual data. Here is the basic structure of the example: const generateColumns = (length = 10, prefix = 'column-', props?: any) => ...

Establishing time intervals for the hours and minutes on Google Charts hAxis using JSON data

I'm working with a Google Line chart that displays the number of tweets over time. However, it currently doesn't show the 30-minute intervals as desired. How can I configure it to display times like 6:30am, 7:30am, and so on? Additionally, how ca ...

How can I align a single button to the left side while positioning the rest of the elements to the right in Bootstrap 4?

I am attempting to align the B1 button on the left side and have the remaining elements positioned on the right side. Below is my code snippet using Bootstrap 4.1: <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/b ...

Error: The getter callback for the component `RNCSafeAreaProvider` must be a function, but it is currently undefined

After attempting to update my React Native app's dependencies using npm update, chaos ensued. To rectify the situation, I reverted back to the previous package-lock.json, deleted the node_modules folder, and re-ran npm i. Surprisingly, instead of res ...

The onClick event for AJAX is malfunctioning

Seeking assistance on using AJAX to send the information from a textarea tag to an email address when the submit button is clicked. Any help is appreciated. Thank you! $(document).on("click", "#submit-btn", function() { ...

Restrict HTML Elements Based on Their Size

I have a text file with a substantial amount of content that I need to display in an HTML format. The challenge is that I only want to show a portion of the text on the screen, but I am unsure of the exact amount that needs to be displayed. What I do know ...