<div style={{display: 'grid'}}>
<button id='plus' onClick={???}>+</button>
<input id='selectImage' type="file" onChange={fileSelectHandler} />
</div>
Here, I want to trigger the onChange function of input, by clicking the button. How to solve this issue?
import React from "react"; function App() { return ( <input type="text" name="firstName" onChange={event => console. log("onchange is triggered")} /> ); } export default App; Now whenever you type something into the text box, React will trigger the function that we passed into the onChange prop.
Create a ref:
//inside the constructor:
fileRef = React.createRef()
// in your input element
<input id='selectImage' type="file" onChange={fileSelectHandler} ref={this.fileRef} />
Now,
<button id='plus' onClick={this.triggerClick}>+</button>
triggerClick() {
this.fileRef.current.click()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With