0

I have an input field and when the button is clicked I would like to store the input to a variable and output it to console. Here is my code:

function InputDD() {
       const [inputValue, setInputValue] = useState('');
       const onChangeHandler = event => {
           setInputValue(event.target.value);
       };
       //console.log(inputValue) <- Not working
     
       return(
       <div>
       <Form>
           <FormGroup>
                <Input type="lk" name="inputK" id="inputK" className="ink" value={inputValue} onChange={onChangeHandler} /> 
           </FormGroup>
           <Button size="sm" className="nextbtn">Lets roll</Button>
<!--Not working -> <Button size="sm" className="nextbtn" onClick={console.log(inputValue)}>Lets roll</Button>-->
       </Form>
       </div>
       )
}

export default InputDD;

Currently, whenever an input is entered, the keys are output in console like in this image: enter image description here

I do not want this and only want the whole text that is input stored to a variable and printed to console when the button is clicked.

1 Answer 1

1
    function InputDD() {
           const [inputValue, setInputValue] = useState('');
           const onChangeHandler = event => {
               setInputValue(event.target.value);
           };
           const handleSubmit = (e) => {
              e.preventDefault()
              console.log(inputValue) //working
           }
    
           
         
           return(
           <div>
           <Form onSubmit={(e) => handleSubmit(e)}>
               <FormGroup>
                    <Input
type="lk" 
name="inputK" 
id="inputK" 
className="ink" 
value={inputValue} 
onChange={(e) => onChangeHandler(e)} 
/> 
               </FormGroup>
               <Button size="sm" className="nextbtn">
Lets roll
</Button>
    <Button size="sm" className="nextbtn" type="submit">
Lets roll
</Button>
           </Form>
           </div>
           )
    }
    
    export default InputDD;
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.