onchange input field, put new value in javascript

You can use the onchange event to detect changes made to an input field, and then use the value property to retrieve the new value and set it to another element or variable.

Here's an example code snippet that creates an input field and a paragraph element, and sets the new value of the input field to the paragraph element when it changes:

<input id="myInput" type="text" onchange="updateValue()">
<p id="myOutput"></p>

<script>
function updateValue() {
  var inputField = document.getElementById("myInput");
  var outputField = document.getElementById("myOutput");
  
  // Get the new value
  var newValue = inputField.value;
  
  // Set the new value to the output element
  outputField.innerText = newValue;
  
  // Or use the new value in some other way
  console.log("The new value is: " + newValue);
}
</script>
479 chars
19 lines

In this example, the updateValue function is called whenever the input field changes, and retrieves the new value using the value property of the input field. It then sets the new value to the paragraph element using the innerText property, and logs it to the console. You can modify this function to suit your specific use case.

gistlibby LogSnag