How to set value of input text using jQuery?

In this tutorial, we'll learn how can we set the value of an input text by default using jQuery?
Submitted by Pratishtha Saxena, on July 22, 2022

For setting the value of an input text, we will use val() method of jQuery.

jQuery val() Method

It is an inbuilt method in jQuery. This method is basically used to set the value of any specified element. This element is specified by mentioning the selector like id or class. This method then sets the value of all the matched elements.

Syntax:

$(selector).val(value);

The value is set using the particular selector.

This method is not only used for setting the values but also for returning the values of the specified element. While returning the value, there is no need to specify the value attribute of the val() method.

But for now, let's see an example for setting the value of an input text.

Example:

HTML:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  </head>
  <body>
    <div class="editor-label">
      <label for="EmployeeId" style="font-weight: bold; font-size: 30px">Employee Number</label>
    </div>
    <br>
    <div class="editor-field textBoxEmployeeNumber">
      <input id="EmployeeId" name="EmployeeId" type="text"/>
      <span class="EmployeeId"></span>
    </div>
  </body>
</html>

jQuery:

<script>
    $(function () {
        $('#EmployeeId').val("0011");
    });
</script>

Output:

Example: set value of input text using jQuery


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.