Is it possible to make a form field inactive based on another input?

I need help with disabling certain form fields until other fields are filled in.

You can check out an example of what I'm trying to achieve here: https://jsfiddle.net/fk8wLvbp/

<!-- 
  Bootstrap docs: https://getbootstrap.com/docs
-->

<div class="container">
 <form class="form-inline">
  <label class="sr-only" for="inlineFormInputName2">Name</label>
  <input type="text" class="form-control mb-2 mr-sm-2" id="inlineFormInputName2" placeholder="Enter your name">

  <label class="sr-only" for="inlineFormInputGroupUsername2">Username</label>
  <div class="input-group mb-2 mr-sm-2">
    <div class="input-group-prepend">
      <div class="input-group-text">@</div>
    </div>
    <input type="text" class="form-control" id="inlineFormInputGroupUsername2" placeholder="Enter a username">
  </div>

  <button type="submit" class="btn btn-primary mb-2">Submit</button>
</form>
</div>

In this particular situation, I want to disable the inlineFormInputGroupUsername2 field if and only if the inlineFormInputName2 field is left empty.

Answer №1

Firstly, it is crucial to have the ability to store the form values using proper logic.

script.js


let formData = {name: '', email: ''};


const handleInputChange = (event) => {
   formData = {...formData, [event.target.name]: event.target.value};
}
    

index.html

<input type="text" class="form-control mb-2 mr-sm-2" id="nameInput" name="name" onChange={handleInputChange} placeholder="John Doe">

<input type="email" class="form-control" id="emailInput" name="email" onChange={handleInputChange}  disabled={formData.name.length > 1} placeholder="johndoe@example.com">

We can achieve this by utilizing the "disabled" attribute.

Note that this is an uncomplicated example and you should separate it into distinct HTML and JavaScript files.

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

The 'file' property of undefined throws an error in ng-file-upload

I am currently exploring the functionality of ng-file-upload from this repository: https://github.com/danialfarid/ng-file-upload I have successfully implemented the basic setup as follows: HTML: <section ng-controller="MyController"> ...

The callback function in AngularJS filters

I'm currently using an AngularJS filter to sort through a list of items. Here is the Jade markup I am using: li(ng-repeat="parcel in parcels | filter : filterActiveAreaParcels") After the filter function runs and the elements are displayed in the DO ...

Utilizing ajax to store information in the database

When my image slider loads the next image, I want to save a record in the database. To achieve this, I am using jQuery and AJAX. I store the necessary information in a hidden field called 'data' so that I can send it to my PHP page. function aj ...

Iteratively sift through data for boolean value

My navigation dynamically retrieves routes from the vue-router, eliminating the need for manual addition. Within these routes, there is a boolean key named "inMenu" in both parent and child routes. I have successfully filtered out the parent routes based ...

An error occurred when attempting to use the getDoc() function from Firebase within Next.js: TypeError - reading 'map' on properties of undefined

Hello everyone at StackOverflow, I ran into a problem while attempting to use .map() on a getDoc() constant in my Next.js application. The error message I'm getting is: "TypeError: Cannot read properties of undefined (reading 'map')". As a n ...

Executing the callback function

I am facing a situation where I have a Modelmenu nested within the parent component. It is responsible for opening a modal window upon click. Additionally, there is a child component in the same parent component that also needs to trigger the opening of a ...

Display values in real-time when the text box is modified

Is there a way to update the total value in a text box based on user input using JavaScript and PHP, or just JavaScript? For example, if item "A" costs $25 and the customer orders 5 of "A", the total should automatically display as $125 when the quantity ...

Handling a WCF POST request with JQuery resulting in a 400 Bad Request HTTP Response

I'm encountering an issue with my JQuery POST not being accepted by the WCF Service. Below is the JavaScript code for the POST: function jqueryPost() { var url = "/LoggingTest"; $.post(url, { message: "test message" }); } To handle the POST ...

Executing a series of functions in succession using jQuery

I am trying to iterate through an object that contains functions which need to execute consecutively. Ideally, I would like these functions to be chained together in a way where one function waits for the previous one to finish before executing (e.g., func ...

Control the access to shared resources when dealing with asynchronous functions in JavaScript

Currently, I am developing a node.js server script that will utilize a shared text list for multiple clients to access asynchronously. Clients have the ability to read, add, or update items within this shared list. static getItems(){ if (list == undef ...

Ensure that you do not proceed with the next step until the promise has been fulfilled

I am currently faced with a challenge in my project where I have a service that wraps a 3rd-party API to retrieve data into my controller. However, I need to perform some data processing to pivot the information before displaying it, but I am struggling to ...

Decoding JSON with JavaScript following the response from JsonConvert.SerializeObject(json) in a .NET handler

I am currently working on a web application using the .NET platform. I have written a Handler code that returns a JSON object to JavaScript (after making an AJAX request). Here is the Handler code: var wrapper = new { left = left.ToString(), t ...

What is the best way to display div elements based on a selected option?

Here is the code for a select list that contains the number of guests for a room: <select name="txtHotelGuestNO" id="txtHotelGuestNO" class="hotels" onchange="show_room_choose()"> <?php for($i=1;$i<=100;$i++) echo "<option value=$i>$ ...

Create a selection menu in an HTML dropdown list using unique values from a JSON object that is separated by commas

I'm working with a JSON object that contains multiple key value pairs, one of which is the Languages key. This Languages key holds comma-separated values such as "English, Hindi, French," and so on. My goal is to extract distinct values from the Lang ...

Creating a dynamic display of flash files on a webpage through a unique combination of PHP, AJAX, and

Despite my lack of experience with Ajax and minimal java knowledge, I have a strong background in SQL and PHP. Even though I anticipate criticism for this question, I am determined to seek help. My primary objective is to rotate 4 flash SWF files randomly ...

Sending a POST request using Node.js Express: A step-by-step guide

Looking for help on sending a post request from node.js Express with data passing and retrieval. Preferably a straightforward method like cURL in PHP. Can anyone assist? ...

Leveraging a VueJS prop as a variable in an array mapping operation

Trying to figure out a solution where a variable (prop) can be used in an array map function. The initial code snippet looks like this: var result = this.$store.getters['example/store'].map(a => a.fixed_column) I aim for fixed_column to be ...

The precise moment for the formation as opposed to the choice of elements

To enhance the appearance of YouTube/Vimeo videos, I am utilizing a technique mentioned in this discussion, which involves creating custom thumbnails for embedded videos. The process is quite straightforward - displaying an image as a placeholder and repl ...

"Convert a date string to a date object using the verbose moment date

I utilized the materialize datepicker to select a date in French format. Now I need to convert this formatted date back to a date object for use in my API. Here's how I attempted to revert the date to a standard format: moment("dimanche 30 juillet 20 ...

A concern arises with security when utilizing jQuery alongside json for requesting data from a service

When calling a service with jQuery, there may be concerns about exposing the service call to savvy users who could intercept it using tools like Fiddler or custom web applications. $.ajax({ url: SERVICE_URL?Q1=eee&Q2=sss, dataType: "application/ ...