Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

trigger a function with onInput intypescript/React

I want to trigger my function elevatorCalc when some numbers are entered in my form field.

const elevatorCalc = () => {
console.log("test")
};

I’m trying something like this using onInput but I cant see "test" in my console

                        <div className="col-md-4" id="number-of-apartments">
                            <label htmlFor="residential_apartments">Number of apartments *</label>
                            <input onInput={() => {elevatorCalc}} required type="number" id="resinput_number-of-apartments" />
                        </div>

any help is greatly appreciated

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use this as a starting point. Note how I pass onInput directly the function name as

onInput={elevatorCalc}

That function receives an HTML Event as input, which I named e
You can access its current value by using e.target.value

const App = (props) => {
  
  const elevatorCalc = (e) => {
    console.log(e.target.value);
  }
  
  return (
    <div className="col-md-4" id="number-of-apartments">
      <label htmlFor="residential_apartments">Number of apartments *</label>
      <input onInput={elevatorCalc} required type="number" id="resinput_number-of-apartments" />
    </div>
  );
};

ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading