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 410 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
"Let us prepare our minds as if we'd come to the very end of life. Let us postpone nothing. Let us balance life's books each day ... The one who puts the finishing touches on their life each day is never short of time."