What is the best way to display an element once an input field with type "date" is populated with a value

Struggling to create a dynamic form that reveals additional fields when a date picker is used? I've searched for solutions, but everything I find involves complex validations and logic that exceed my needs.

I've had some success with revealing elements using :checked for radio inputs. Is there a similar method that works for date inputs? I'm open to using jQuery as well, especially since I'm already using it to prevent selecting past dates.

$(function(){
 var dtToday = new Date();
 var month = dtToday.getMonth() + 1;
 var day = dtToday.getDate();
 var year = dtToday.getFullYear();
 if(month < 10)
 month = '0' + month.toString();
 if(day < 10)
 day = '0' + day.toString();
 var maxDate = year + '-' + month + '-' + day;
 $('#date').attr('min', maxDate);
});
.when, .form {display:none;}
input:checked ~ .when {display:block;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>

<h4>Are you sending or receiving?</h4>
<input type="radio" id="sender" name="clienttype"><label for="sender">Sending</label>
<input type="radio" id="receiver" name="clienttype"><label for="receiver">Receiving</label>

<div class="when">
 <h4>When?</h4>
 <input type="date" id="date" name="Date">
</div>

<div class="form">
 Additional form fields can be added here
</div>

Answer №1

If you're looking to capture the value when the date changes, consider using the jQuery change() event

$('#date').change(function() {
    let selectedDate = $(this).val();
    console.log(selectedDate);
});

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

Monitoring Logfile in Ruby On Rails 3.1

Within my Ruby on Rails application, I have a set of scripts that need to be executed. In order to ensure they are working properly, the application must display and track the content of logfiles generated by these scripts. To provide more context: I util ...

Creating a text design that spans two lines using Scalable Vector Graphics (SVG

I am working with an SVG that displays strings pulled from an Array... {"label":"Here is an example of the string...", "value":4}, The text above is shown in an SVG element as... <text>Here is an example of the string...<text> I would like ...

The Fab Button from Material UI remains beneath the left split-pane

Is there a way to ensure the Fab button is completely visible? The left side often gets hidden under the left pane. I attempted to increase the z-index, but it did not have any effect. View on CodeSandbox <UpperPane> <Fab ...

Creating a feature that uses a button press to initiate an Ajax request based on a specific identifier

I'm looking for the most efficient way to structure a page that involves making Ajax calls. My main issue lies in setting up the event binding for when a user clicks a button. I'm struggling to find a method to pass the ID to the function that t ...

A step-by-step guide on extracting JSON data from PHP and presenting it in a formatted list

I am working on a simple jQuery AJAX form submission. When the form is submitted, my PHP script echoes json_decode($result). Below is my AJAX script: <script> $("#ajaxquery").live( "submit" , function(){ // Intercept the for ...

Building a Table Using HTML and CSS

I'm currently tackling this school assignment and have hit a roadblock with two issues. The HTML Validation error I'm encountering is regarding a table row that is 5 columns wide, exceeding the established count by the first row (4). From line 7 ...

Is using JSONP the only choice for an XML API in a jQuery ajax call?

Currently, I am working in jQuery and attempting to make an Ajax call to query an external XML API located on a different domain. Unfortunately, my requests are being blocked due to the cross-domain restrictions. I am curious about alternative options fo ...

What is the best way to set the tab as "active" in the first iteration of a PHP while loop?

When using a while loop to populate tabs with PHP, maintaining only one active tab at a time can be tricky. I have grasped that assigning the 'active' class within the loop causes all tabs to become active. So, how should I modify the script if I ...

I am unable to see the text field when I use element.text. What could be the reason for this

As I work on collecting online reviews using Selenium Chrome Webdriver, I've encountered a challenge with TripAdvisor. The reviews are truncated and require clicking a "More" button to view the complete text. Within the html code, there is a class cal ...

Unlocking two features with just a single tap

I am currently working on a website for a kiosk, where the site transitions like a photoslide between each section. To achieve this effect, I have added a layover/mask on the initial page. The layover/mask is removed using a mouse click function. This is ...

How can we reduce the use of !important in SCSS when working with React and Material UI?

In my current project, I am developing a Select component in React with Material UI. The CSS styling for the component is managed through an external SCSS sheet imported into the script file. While working on restyling the component, I found it challengin ...

Utilizing AMD Modules and TypeScript to Load Bootstrap

I am attempting to incorporate Bootstrap into my project using RequireJS alongside typescript AMD modules. Currently, my requireJS configuration looks like this: require.config({ shim: { bootstrap: { deps: ["jquery"] } }, paths: { ...

What are the best ways to ensure a website is responsive in a vertical

When designing a responsive webpage, I encountered an issue with the body element not properly expanding the content vertically. Despite enlarging the web content horizontally, the body failed to adjust its height accordingly, resulting in unwanted black a ...

What is the reason for the excessive width of the final column in this table?

I am currently working with a dataset that I am displaying using the handsontable library in the form of a table. One issue I am facing is that the last column of my table appears too wide even though I did not specify any width for it. Below you can see t ...

Issues with JSON data retrieved via Ajax requests

Recently, I created a page on WordPress with the intention of submitting a form via AJAX. However, every time the form is submitted, the URL changes to the form handler's URL and instead of using the JSON data to update the current page, it simply dis ...

The ng-bind directive is functional when used with a textarea element, but it does not work with

Recently diving into the world of angularjs, I stumbled upon ng-bind and its interesting functionality when used as a directive for textarea: <input type="text" ng-model="review.start"/> <textarea ng-bind="review.start"> </textarea> I ...

What is the best way to eliminate empty space at the bottom of a page that is being caused by a button?

I have identified the reason for the blank space at the bottom of my page - it's due to a sticky "back to top" button. However, I am unsure how to resolve this issue. Here is the layout of the page: <nav> navbar </nav> <div> car ...

Retrieve all elements from JSON using jQuery

JavaScript: function loadDoc(url) { $.ajax({ url: 'mytestjson', dataType: 'json', cache: false }).success(function (result) { console.log(result); //var txt = result.newBranches[0].newNon ...

Storing the AJAX response in an external variable using jQuery callback function

I'm struggling to set a variable with the results of an ajax query. I understand that I can't just return the result in the success function due to its asynchronous nature. However, I'd like to achieve this using a callback function without ...

The website's responsive design functions flawlessly when viewed on a desktop browser or mobile-simulator for Safari and Mozilla Firefox. However, when accessed on an actual smartphone using Android or iOS

I've been experimenting with different lines of code, but I'm struggling to pinpoint the error as my code functions on a desktop and should also work on mobile devices... <meta name="viewport" content="width=device-width, initial-scale=1, max ...