Languages
[Edit]
EN

jQuery - how to set value of input field?

1 points
Created by:
JustMike
3482

Using jQuery it is possible to set input field value in following way.

Short solution

$('#my-input').val('This text has been set from javascript code...');

$('#my-input').attr('value', 'This text has been set from javascript code...');

1. Input val method example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>  
  
    input {
    	padding: 10px;
      	width: 300px;
    }
    
  </style>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
</head>
<body>
  <input id="my-input" type="text" value="" />
  <script>

    var input = $('#my-input');
    
    input.val('This text has been set from javascript code...');

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

2. Input attr method example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>  
  
    input {
    	padding: 10px;
      	width: 300px;
    }
    
  </style>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
</head>
<body>
  <input id="my-input" type="text" value="" />
  <script>

    var input = $('#my-input');
    
    input.attr('value', 'This text has been set from javascript code...');

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

See also

  1. JavaScript - how to set value of input field?

Checkout latest findings & news:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Rockstar community members
Thank you for great work 👍