Re-sending keyup event for DATE input in JavaScript

Is there a way to redirect the keyup KeyboardEvent from one native date input element to another in real time, similar to typing in two date inputs simultaneously?

I experimented with this code using a text input, and it worked perfectly.

However, when I attempted to apply the same concept to a date input, it didn't seem to work as expected: even though the event was dispatched, nothing appeared in the second date input field.

Here is the JSFiddle link for the code demonstration

This is how my code looks like: index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="main.js" defer></script>
</head>
<body>

  <input id="startDate" type="date" />
  <input id="endDate" type="date" />

  <input id="text1" type="text" />
  <input id="text2" type="text" />
</body>
</html>

main.js

let startDate = document.getElementById('startDate'),
    endDate = document.getElementById('endDate'),
    text1 = document.getElementById('text1'),
    text2 = document.getElementById('text2')
;

let endDateBinded = this.dispatchElementEvnt.bind(null,[endDate]),
    text2Binded = this.dispatchElementEvnt.bind(null,[text2]);

startDate.addEventListener('keyup', endDateBinded);
endDate.addEventListener('keyup',selfUpdate);

text1.addEventListener('keyup', text2Binded);
text2.addEventListener('keyup',selfUpdate);

function dispatchElementEvnt(secondElement,e){
    let event = new KeyboardEvent(e.type,e);
    secondElement[0].dispatchEvent(event);
}

function selfUpdate(e) {
    this.value += e.key;
}

Answer №1

The problem lies with the startDate being initially set to an invalid date format (i.e. dd/mm/2021) before it is corrected to a valid date (i.e. 12/8/2021). Unfortunately, both dates will not update in real time simultaneously, however, they can be updated once the startDate has been rectified.

credit: Modifying two input type dates at once

The following code snippet automatically adjusts the endDate when the StartDate is modified:

let startDate = document.getElementById('startDate'),
    endDate = document.getElementById('endDate'),
    text1 = document.getElementById('text1'),
    text2 = document.getElementById('text2')
;

let endDateBound = this.dispatchElementEvent2.bind(null,[endDate]),
    text2Bound = this.dispatchElementEvent.bind(null,[text2]);

startDate.addEventListener('change', endDateBound);
endDate.addEventListener('keyup', selfUpdate2);

text1.addEventListener('keyup', text2Bound);
text2.addEventListener('keyup', selfUpdate);

function dispatchElementEvent(secondElement, e){
    let event = new KeyboardEvent(e.type, e);
    secondElement[0].dispatchEvent(event);
}

function selfUpdate(e) {
    this.value += e.key;
    
}
function dispatchElementEvent2(secondElement, e){
    let event = new KeyboardEvent(e.type, e);
    secondElement[0].dispatchEvent(event);
    endDate.value = startDate.value;
}

function selfUpdate2(e) {
    this.value += e.key;
    
}

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

Is there a way to simulate the parameters injected into an fs callback without actually interacting with the filesystem during testing?

I'm currently utilizing Chai, Mocha, and Sinon for my testing framework. At the moment, I have a functioning test, but I find myself having to set up a directory and populate it with files just to make my tests run successfully. This approach doesn&a ...

Unable to establish a connection with the TCP server on CloudFoundry, although the localhost node.js is functioning properly

I am experiencing difficulty connecting to my TCP server example that is running on CloudFoundry. Interestingly, when I run my app.js file on a local node.js installation, everything works perfectly. Upon using vmc push to deploy on CloudFoundry, the servi ...

Tips on handling a Vue computed property

I am struggling to dispatch an object that is created within a computed property in vue.js. Being fairly new to this framework, I can't seem to make it work. My goal is to dispatch the object named "updateObject" to the vuex-store. I have tried using ...

Exchange one HTML element with a different HTML element

I've been attempting to change an HTML tag using PHP or jQuery. The current default tag is: <li class="dropdown"> <a href="index.html" class="dropdown-toggle"> Home</a></li> My desired replacement for the above HTML tag is: ...

Encountering a hitch while attempting to integrate a framework in Angular JS 1

Having experience with Angular JS 1 in my projects, I have always found it to work well. However, I recently encountered a project that uses Python and Django, with some pages incorporating Angular. The specific page I needed to work on did not have any An ...

"Experience a unique website layout with varying designs on desktop and mobile devices while using the React technology to enable desktop

Just starting out with React and I've noticed something strange. When I view my website on a PC, everything looks normal. However, when I switch to the desktop site on my phone, things appear differently. It seems like moving an element by 20px on mob ...

Ways to terminate session using ajax when input is empty; Notice of an undefined variable

Warning: Variable 'outputname' is undefined There is a query in the file memberSearch.php. I want to echo the $outputname variable in another PHP file inside an input tag. However, when I try to echo ($outputname) in another file, it returns an ...

Is it possible to stop the manipulation of HTML and CSS elements on a webpage using tools similar to Firebug?

How can we prevent unauthorized editing of HTML and CSS content on a webpage using tools like Firebug? I have noticed that some users are altering values in hidden fields and manipulating content within div or span tags to their advantage. They seem to be ...

What is the best way to show a message of success once the user has been redirected to the homepage?

Currently, I have a registration form utilizing AJAX and PHP for validation. Error messages can be displayed on the registration page if the user does not correctly fill out the form. Upon successful registration, the user is redirected back to the home pa ...

Issue: React child must be a valid object - Runtime Error Detected

As I delve into the world of React, NextJs, and TypeScript, I stumbled upon a tutorial on creating a navbar inspired by the 'Strip' style menu. It has been quite a learning journey for me as a newbie in these technologies. After seeking help for ...

The value of $index remains constant for every page that utilizes dir-paginate

Currently, I am utilizing the dirPaginate directive to handle pagination within my Angular application. While it is functioning correctly overall, I have encountered an issue where each page displays 10 records but the $index values are not incrementing ...

Is there a way to fetch a random record from a MongoDb collection and exhibit all the fields of that haphazardly chosen document in HTML?

In my current project, I am fetching a random document from a MongoDB Collection and attempting to showcase all the fields of that document in HTML. Although I can successfully retrieve a random document, the issue arises when trying to display its fields ...

Shader designed to maintain the original lighting of the landscape

Currently, I have a shader set up to mix various textures for my terrain such as grass, sand, and rocks. While the blending is working well, the issue arises with compatibility with Three.js lighting engine. All vertices appear overly bright due to this ...

Tracking Clicks on Folders in jQuery File Tree

I am attempting to integrate a jQuery file tree into my website using the example provided below: Check out this jQuery file tree example While I can successfully trigger an event when a file is clicked, as demonstrated in the example, I am struggling to ...

Improving the efficiency and readability of JavaScript syntax (specifically conditional statements with if-else) in Vue.js

Seeking advice on optimizing and simplifying this syntax in Vue's methods section: methods: { getColorBtn(status, isCorrect, isRemind, textButton) { if (status === 1 && isCorrect === 1 && isRemind === 1) return 'v-btn--outlined theme--l ...

I would like to see a straightforward demonstration of how DOM manipulation is done by the Controller in AngularJS

As a newcomer to AngularJS, I keep hearing warnings about not using controllers to handle DOM manipulation. I understand the importance of flexibility, but I would appreciate some concrete examples to illustrate this concept. Can you clarify whether DOM ...

The Vue.js class bound to the object remains static even after the object is updated

When I have a number of buttons, generated using the v-for directive, with each button having an initial class based on a string from an object. There is an event that changes this string when a button is clicked. However, the class does not get updated a ...

Utilizing jQuery's Ajax functionality to extract filtered data from mySQL

I've been working on sending query strings fetched by clicking radio buttons to the server in order to receive a response in XML format. Although I'm close to finding a solution, I'm struggling to debug why it's not functioning as expec ...

Build a row within an HTML table that is devoid of both an ID and a class

Creating an additional row in an HTML table is a common task, but it can become tricky when dealing with multiple forms and tables on one page. In my case, I have a form that contains only a table element, and I want to move some columns from the first row ...

"Encountering issues with toggle effect in nested divs when utilizing an Angular Directive -

I have included a link to the Plunker. Unfortunately, I am facing issues with accessing the elements .gc and .mc. On the other hand, the class .bc is functioning properly. My goal is to implement a toggle effect on them based on hierarchy: BroadCategory ...