how to access inputs in a js form code example

Example 1: input js

var answer = prompt("Ask your question here");

Example 2: js get value from form input

// having form like this

<form>
  <div>
  	<label for="number">Give me number</label>
	<input type="number" id="number">
  </div>
  <div>
    <button class="btn">Click</button>
  </div>
</form>

// this is how u get a value (number)
document.querySelector("form").addEventListener("submit", get_number);
function get_number(e){
    const n = document.getElementById("number").value;
    console.log('n:', n)
    e.preventDefault();
}

Example 3: javascript input field

<!DOCTYPE html> 
<html> 
  <body>
    <input> Here is your inputfield </input>
  </body>
</html>

Tags:

Html Example