1

I want to manually trigger a onclick event on a input element. I did some search and wrote the code as below.

<input type="date" ref={datePicker => this.datePicker = datePicker}/>
            <button
                    onClick={(e) => {
                      this.datePicker.click()
                    }}>
             Select the data
            </button>

Then I also tried below code:

<input type="date" ref='datePicker'/>
                <button
                        onClick={(e) => {
                          this.refs.datePicker.click()
                        }}>
                 Select the data
                </button>

None of above code works. When I click the button , nothing happens. I expect to open the data picker ui.

5
  • Can you use jQuery? Commented Dec 15, 2016 at 0:20
  • @Ro If reactjs supports this I don't want to bring another dependency into my application Commented Dec 15, 2016 at 1:10
  • @CodeBling The datePicker is defined in the <input> tag. I want to show the date picker triggered by the input tag. Commented Dec 15, 2016 at 1:11
  • @CodeBling It is already there. Look at the first line of my code. The <input> tag has a property ref which point to datePicker. Commented Dec 15, 2016 at 1:38
  • I've updated my answer, sorry for the confusion. Commented Dec 15, 2016 at 3:22

2 Answers 2

1

I tried both your examples, and the <input> element's .click() method is indeed called. The reason it's not working as you expect is that it the click only puts the keyboard focus in the element, it does not click the date picker. Opening the date picker programmatically is currently not possible, at least not in all browsers.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there any way for me to customize the <input type='date'> ui?
See stackoverflow.com/a/15531938/675721. I think you can make minor adjustments, but not really. See also developers.google.com/web/updates/2012/08/…
0

Try without calling it as a function:

onClick={(e) => {this.datePicker.click}}>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.