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

Write to Paragraph from Text Inputs with React

I am working on some code that takes the inputs from two textboxes, one that takes a string value and one that takes a number value, and in real time writes it to a paragraph without a submit button. I am having trouble finding any information on this process without submitting anything. The paragraph should just display . To begin, this is what I have:

import './App.css';

function App() {
  return (

    <div>
      <form>
        <div>String:</div>
      <input type="text" strng = "strng" /><br/><br/>
        <div>Number:</div>
      <input type="text" strng = "number"/><br/><br/>
      <button>Clear</button>
      <p>
        Inputs: 
      </p>
      </form>
    </div>
  )
  }
  export default App;

Would someone be able to help me out with writing the two text boxes to the paragraph in real time? Thanks so much in advance.

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 should be able to bind a useState with the onChange event of the input

import './App.css';
import { useState } from "react";

function App() {
  const [text, setText] = useState("");

  return (
    <div>
      <form>
        <div>String:</div>
        <input
          type="text"
          strng="strng"
          onChange={(e) => setText(e.target.value)}
        />
        <br />
        <br />
        <div>Number:</div>
        <input type="text" strng="number" />
        <br />
        <br />
        <button>Clear</button>
        <p>Input: {text}</p>
      </form>
    </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