Get existing instance of EJ2 DatePicker control

I am trying to find out how to retrieve the existing instance of a DatePicker (or any of them) control and then set a value through standard javascript.  I believe on the JS1 version, it was done something like this:

datepickerSet = function (dp, date) {
            var b = getObject(dp), d = new Date(date);
            if (exists(b)) {
                var ejdp = b.ejDatePicker('instance');
               ejdp.option('value', d);
            }
           
        }

How would I go about doing this with EJ2?  I am creating the original instance with MVC5 Render(), but need to access and update it after it has been rendered to the page.  I found the following link to use "value" method, but doesn't show how to get the instance of something already rendered. 
https://ej2.syncfusion.com/15.4.17/documentation/datepicker/api-datePicker.html?lang=es6#value-date


1 Reply

SP Sureshkumar P Syncfusion Team April 3, 2018 04:35 PM UTC

Hi Kyle, 
 
Thank you for contacting Syncfusion support, 
 
As like in JS1 version, you can also get the instance of the existing EJ2 DatePicker component to set a value after initialize. In below code snippet, we have showcased how to retrieve the instance of DatePicker component and setting the value on clicking the ‘update value’ button. 
 
[sample.html] 
 
            <input id="datepicker" type="text"><br><br> 
            <button id='btn'>Update the Value</button>  
[sample.js] 
 
/** 
* Default DatePicker sample 
*/ 
 
let datepickerObj = new DatePicker(); 
datepickerInstance.appendTo('#datepicker'); 
 
// fires  when button element clicked 
document.getElementById('btn').addEventListener('click', function() { 
    //datepickerInstance is the instance of our created datepicker element 
    datepickerObj.value = new Date(); 
}); }); 
Similar way, you can get the instance of all EJ2 component. For your convenience we have attached a simple sample based on your requirement in below link 
 
Regards, 
Sureshkumar P 


Loader.
Up arrow icon