Languages
[Edit]
EN

JavaScript - access input element

11 points
Created by:
jarlh
635

In this article, we would like to show you how to access input element using JavaScript.

Short solution

var input = document.getElementById('my-input');
input.value = 'This text has been set from javascript code...';

var input = document.forms['my-form']['my-input'];
input.value = 'This text has been set from javascript code...';

var forms = document.forms['my-form'];
var input = form.elements['my-input']; // form.elements.namedItem('my-input');
input.value = 'This text has been set from javascript code...';

var form = document.getElementById('my-form');
form['my-input'].value = 'This text has been set from javascript code...';

1. Input id attribute example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>  
  
    input {
    	padding: 10px;
      	width: 300px;
    }
    
  </style>
</head>
<body>
  <input id="my-input" type="text" value="" />
  <script>

    var input = document.getElementById('my-input');
    
    input.value = 'This text has been set from javascript code...';

  </script>
</body>
</html>

2. document.forms property example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>  
  
    input {
    	padding: 10px;
      	width: 300px;
    }
    
  </style>
</head>
<body>
  <form name="my-form">
    <input id="my-input" type="text" value="" />
  </form>
  <script>

    var forms = document.forms;

    var form = forms['my-form'];
    var input = form['my-input'];

    input.value = 'This text has been set from javascript code...';

  </script>
</body>
</html>

3. Form elements property example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>  
  
    input {
    	padding: 10px;
      	width: 300px;
    }
    
  </style>
</head>
<body>
  <form name="my-form">
    <input id="my-input" type="text" value="" />
  </form>
  <script>

    var form = document.forms['my-form'];
    var input = form.elements['my-input']; // form.elements.namedItem('my-input');

    input.value = 'This text has been set from javascript code...';

  </script>
</body>
</html>

Note: with this approach we can access easly input elements when name conflict occured. 

4. Form id attribute example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>  
  
    input {
    	padding: 10px;
      	width: 300px;
    }
    
  </style>
</head>
<body>
  <form id="my-form">
    <input id="my-input" type="text" value="" />
  </form>
  <script>

    var form = document.getElementById('my-form');
    var input = form['my-input'];

    input.value = 'This text has been set from javascript code...';

  </script>
</body>
</html>

Alternative titles

  1. JavaScript - how to access input element?
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join