Languages
[Edit]
EN

jQuery - set value of input field

1 points
Created by:
Kenya-Spears
800

In this article, we would like to show you how to set input field value in using jQuery.

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?

Alternative titles

  1. jQuery - how to set value of input field?
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.

jQuery

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