Activate a disabled element in Vue.js Datepicker

In my scenario, there are 2 instances of the <date-picker>. The first instance is used for capturing the start date (starts_at), while the second instance captures the end date (ends_at).

According to the design I have envisioned, I would like to initially disable the ends_at instance and then re-enable it once the starts_at has been selected.

Here is the code for my starts_at instance:

<date-picker v-model="starts_at" @input="enableEndsAt()" valueType="format"></date-picker>

And here is the code for my ends_at instance:

<date-picker id="endsAt" v-model="ends_at" disabled @input="checkDate()" valueType="format"></date-picker>

I have attempted an approach where changing the input triggers the enableEndsAt() function. Here is how the function looks:

enableEndsAt(){
  if(this.starts_at === "") {
    return
  }
  var element = document.querySelector('#endsAt')
  element.removeAttribute('disabled')
  console.log(element)
}

Answer №1

You have the option to utilize disabled as a dynamic attribute:

<date-picker id="endsAt" 
     v-model="ends_at" 
     :disabled="disableDatePicker" 
     @input="checkDate()" 
     valueType="format"></date-picker>

To implement this, you can define a disableDatePicker property in your data section, and then modify it within your method:

data(){ return { disableDatePicker: false }},
...
methods: { ....
enableEndsAt(){
  if(this.starts_at === "") {
    return
  }
  this.disableDatePicker = true;
}

Answer №2

If your <date-picker> has the capability to accept the disabled property, you can effortlessly link it to a specific condition:

<date-picker id="endsAt" v-model="ends_at" :disabled="this.starts_at === ''" @input="checkDate()" valueType="format"></date-picker>

This eliminates the need for adding

@input="enableEndsAt()"
onto your starts_at date picker, as well as getting rid of the enableEndsAt() function.

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

What steps are involved in linking Java with JavaScript?

After attempting to connect html and javascript using applet, unfortunately it was unsuccessful. I am curious if there is an alternative method besides applet to establish this connection. ...

What is causing the label's color to remain the same?

Once the page has loaded, the label color (which reads "enter your name") is supposed to change to red. However, despite the script being in place, the color remains unchanged. What could be the reason for this? SCRIPT window.onload = initiateScript; fu ...

Guide on Showcasing Countries on a Global Map with Various Colors Using react-svg-map

I'm currently working on a project that involves highlighting specific countries on a world map. The default color for the countries is light blue, but I need to change it to dark blue for the highlighted ones. Although the library I'm using has ...

Is there a way to ensure the content of two divs remains aligned despite changing data within them?

Currently, I have two separate Divs - one displaying temperature data and the other showing humidity levels. <div class="weatherwrap"> <div class="tempwrap" title="Current Temperature"> ...

AngularJS is patiently waiting for the tag to be loaded into the DOM

I am trying to incorporate a Google chart using an Angular directive on a webpage and I want to add an attribute to the element that is created after it has loaded. What is the most effective way to ensure that the element exists before adding the attribut ...

Retrieving a specific Project ID from Asana Task API using Node.js and parsing the JSON response

Utilizing the Asana Task API, we have the capability to retrieve a task's associated projects along with their GID and Notes (description text). The main objective Our aim is to extract the GID of the project containing #websiteprojecttemplate in its ...

Using Vue.js to inject plain text into a Vue component from HTML

I am currently in the process of transitioning code from ASPX to VUE.JS. In the previous system, there was a feature that allowed plain text to be injected into HTML (such as messages with text, images, links, and inputs). However, in VUE, the injected HT ...

Glitch in transmitting server data to client in MERN stack development

I am currently developing a MERN web application and I've encountered a bug in which the data retrieved from the server is not matching what is expected when making a GET request on the client side. Below is the controller function on the server for ...

Scheduling a cron job to run at a particular date and time

Our express js application includes a feature in the admin module where users can send emails at specific dates and times they select. For example, if the chosen date and time is [email protected], we need to execute the email code at that exact time ...

Issue encountered when attempting to remove items from Redux store with onClick event

My goal is to delete a specific object from an array in my store. I have a delete item function that successfully removes objects, but I am struggling to make it work with a button that is rendered with each object using map. Here is my component: import ...

Display multiple elements within a single component in vuejs

One of the challenges in Vue.js is looping out a number of boxes within the same component. Each box contains a button that reveals more text using the v-on:click directive. The issue arises when all the boxes respond to this event simultaneously. Is ther ...

Converting JSON data into a JavaScript array and retrieving the array through an AJAX request from PHP

Currently, I am working on a project where I have created a function named AjaxRequest to manage all my AJAX requests. While making the requests is not an issue, receiving and placing the data back onto the page has proven to be quite challenging. Within ...

"Utilize PHP to access an object's properties in a manner similar to an array

Consider the JSON object presented below: $json = '{ "Name": "Peter", "countries": { "France": { "A": "1", "B": "2" }, "Germany": { "A": "10", "B": "20" }, .... } }'; I am looking for a way to ...

Vue unable to properly use .length on array within a style tag

The code snippet I'm using is as follows: <div v-for="(item, index) in data.items" :style="'display:inline-block !important; padding-left: 2% !important; padding-right: 2% !important; width: ' + data.items.length + '% !i ...

NextJS displays outcomes as per query parameters obtained from an external API

I have set up my NextJS app to connect to a rest API. The API returns results based on different filters in the query string, such as: /jobs /jobs?filter=completed /jobs?filter=upcoming /jobs?filter=cancelled On my NextJS page, I have a few buttons that I ...

Add a JSON file containing an image path as a value into a CSS background property

I currently have a MongoDB database containing documents with 'img' values structured as follows: "img": "../folder/img.jpg" Would it be feasible to utilize this string in my CSS for modifying the background image? This is essential because I n ...

When trying to import SVGs using gatsby-plugin-react-svg, an error message stating "InvalidCharacterError: String contains an invalid character" is received

After realizing that my website's Largest Contentful Paint (LCP) might be affected by font-awesome svg icons, I decided to replace them. I extracted the svg code from my html and saved them as svg files. They appeared as images in my image editing sof ...

Is there a way to use jQuery to animate scrolling on a mobile web browser?

I'm trying to make the page scroll to a specific position when a button is clicked. The code I currently have works fine on browsers like Chrome and IE, but doesn't seem to work on any mobile browser. Below is the code snippet I am using: $("#p ...

The Jasmine test does not instantiate a new class instance

I've been working on incorporating a Jasmine test into my JavaScript program (which is just a game), but I'm running into an issue where new instances aren't being created. I'm hoping that someone could help me troubleshoot and identify ...

What is the most effective way to import a substantial static array in React for utilization in a select field?

The scenario is quite straightforward. I currently have a single array containing over 2500 strings of company names, saved locally in the project as a JSON file within a subdirectory under src. To access this data in my component, I am importing the JSON ...