0

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

1
  • 2
    You're not calling the function at the moment. onInput={() => {elevatorCalc}} needs to be onInput={() => {elevatorCalc()}} (or onInput={elevatorCalc}) Commented Mar 21, 2022 at 20:24

1 Answer 1

1

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>

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.