EN
jQuery - set value of input field
1 points
In this article, we would like to show you how to set input field value in using jQuery.
xxxxxxxxxx
1
$('#my-input').val('This text has been set from javascript code...');
2
3
$('#my-input').attr('value', 'This text has been set from javascript code...');
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
6
input {
7
padding: 10px;
8
width: 300px;
9
}
10
11
</style>
12
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
13
</head>
14
<body>
15
<input id="my-input" type="text" value="" />
16
<script>
17
18
var input = $('#my-input');
19
20
input.val('This text has been set from javascript code...');
21
22
</script>
23
</body>
24
</html>
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
6
input {
7
padding: 10px;
8
width: 300px;
9
}
10
11
</style>
12
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
13
</head>
14
<body>
15
<input id="my-input" type="text" value="" />
16
<script>
17
18
var input = $('#my-input');
19
20
input.attr('value', 'This text has been set from javascript code...');
21
22
</script>
23
</body>
24
</html>