get the value of an input form element (input value)

add how to get the value of an input form element with javascript

for some reason i can get this working with:



but when trying to assign it to a variable, it gives the error:

maybe the script is running befor the document is fully rendered so this does not yet have a value.

probably need to attach this to either a function that can be run by the user or a wait until the document is ready. 

The function onclick version works. 

Also try the doc ready event detailed here: document ready javascript only 



<input type="text" value="my value" id="my_input">
<button onclick="get_input_val();" class='btn btn-primary'>Get Input Value</button>


// let my_input_val = document.getElementById("my_input").value;
// var my_input_val = document.getElementById("my_input").value;
// console.log(my_input_val);

function get_input_val() {
  var my_input_val = document.getElementById("my_input").value;

document.addEventListener("DOMContentLoaded", function(event) { 
	var my_input_val = document.getElementById("my_input").value;

Other Items in javascript

No Items Found.

Add Comment
Type in a Nick Name here
Page Views

This page has been viewed 366 times.

Search Code
Search Code by entering your search text above.

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.


Random Quote
Our brains are wired to find things we are looking for - if your always cynical or waiting for things to go wrong, then your life will reflect that. On the other hand, having a positive outlook on life will bring you joy and provide you with inspiration when you least expect it β˜€οΈπŸ‰πŸŒ΄