Top community members
All Wiki Articles Create Wiki Article

In the past we had a dream,
to have place where we could share IT knowledge,
to ask questions without fear that someone will judge us.

Now we are a group of people who make this dream come true. ❤ 💻

If you think sharing knowledge and helping other is valuable.

join our community - Click here

JavaScript - how to access input element?

0 contributions
11 points

In JavaScript it is possible to acces input element in following way.

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>
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more