• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / JavaScript / JavaScript value – Get the Value from an Input Field

JavaScript value – Get the Value from an Input Field

February 5, 2022 Leave a Comment

To get the value from an input field, we can use the value property. We can also use the value property to set the value of an input field.

var userInput = document.getElementById("input1").value;

Let’s say we have the following HTML:

<form>
  <label for="fname">Full Name:</label>
  <input type="text" id="fname" name="fname">
  <button type="submit" value="Submit">Submit</button>
</form>

To get the value that the user has entered for “Full Name”, we would use the value property.

document.getElementById("fname").value;

This can also be done using jQuery with the val() method.

Get and Set an Input Field using JavaScript and the value Property

In this example, we will have a form that will display the information of a user. We will then let the user update one of the fields by using an input field and submit button below it.

Here is the HTML setup:

<form id="form-1">
  <label for="username">Username:</label>
  <input type="text" id="username" name="username" value="theprogrammingexpert" disabled>
  <label for="pass">Password:</label>
  <input type="password" id="pass" name="password" value="password" disabled>
</form>
<div id="div1">
  <label for="userinput">Enter new username:</label>
  <input type="text" id="new-username" name="userinput">
  <div id="click-me" onclick="updateUsername()">Update</div>
</div>

In the JavaScript code below, we will get the user input using the value property, and then set the input value “Username” in the form above using the value property again along with the getElementById method.

Here is the JavaScript code for getting and setting an input field:

function updateUsername(){
  var userInput = document.getElementById("new-username").value;
  document.getElementById("username").value = userInput;
};

The final code and output for this example of getting and setting an input field using JavaScript and the value property is below:

Code Output:






Update

Full Code:

<form id="form-1">
  <label for="username">Username:</label>
  <input type="text" id="username" name="username" value="theprogrammingexpert" disabled>
  <label for="pass">Password:</label>
  <input type="password" id="pass" name="password" value="password" disabled>
</form>
<div id="div1">
  <label for="userinput">Enter new username:</label>
  <input type="text" id="new-username" name="userinput">
  <div id="click-me" onclick="updateUsername()">Update</div>
</div>

<script>

function updateUsername(){
  var userInput = document.getElementById("new-username").value;
  document.getElementById("username").value = userInput;
};

</script>

Hopefully this article has been useful to help you understand how to use the value property to get the value from an input field.

Other Articles You'll Also Like:

  • 1.  JavaScript Trunc with Math.trunc() Method
  • 2.  Unveiling the Magic: JavaScript Show and Hide Div
  • 3.  Adding an Underline with JavaScript
  • 4.  How to Clear an Input Field in JavaScript
  • 5.  Using JavaScript to Count Even Numbers in an Array
  • 6.  JavaScript Check If Number is a Whole Number
  • 7.  JavaScript join – Create String from Elements in an Array
  • 8.  Using Javascript to Check if Value is Not Null
  • 9.  Using JavaScript to Get Today’s Date
  • 10.  Using JavaScript to Generate a Random Float Between 0 and 1

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy