how do i save the input.value while typing into a new variable(currentInput) so that i can use that new variable globally?
HTML code:
<h1>Check if your text is a Palindrome:</h1>
<div id="palindrome-checker">
<p>Enter your text</p>
<form id="palindrome-form">
<input type="text" id="text-input" required value="">
<button type="button" id="check-btn" >Enter</button>
</form>
Javascript code:
const input = document.querySelector("#text-input");
const button = document.querySelector("#check-btn");
const output = document.querySelector("#result");
const form = document.querySelector("#palindrome-form");
let currentInput = "";
input.onkeyup = myFunction
function myFunction(){
currentInput = document.querySelector("#text-input").value
}
2
Answers
maybe this would be better code:
So, this would be the straight forward answer: You can listen for the input event on the input element and update the variable
currentInput
:But, why have that extra variable when there is already a variable with the value. From anywhere in you JavaScript code you can ask for:
form.text_input.value
. That said, I guess you would like to update something when the value change. To either just add more event listeners:Or create a custom event when the value updates: