Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • jQuery Tutorial
  • jQuery Selectors
  • jQuery Events
  • jQuery Effects
  • jQuery Traversing
  • jQuery HTML & CSS
  • jQuery AJAX
  • jQuery Properties
  • jQuery Examples
  • jQuery Interview Questions
  • jQuery Plugins
  • jQuery Cheat Sheet
  • jQuery UI
  • jQuery Mobile
  • jQWidgets
  • Easy UI
  • Web Technology
Open In App
Next Article:
jQuery width() Method
Next article icon

jQuery val() Method

Last Updated : 10 Jul, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The val() method is an inbuilt method in jQuery that is used to return or set the value of attributes for the selected elements. This method applies to the HTML form elements.

Syntax: There are three ways to use this method which are given below: 

$(selector).val()

Parameters: This method does not accept any parameters.

$(selector).val(value)

Parameters: This method accepts a single parameter value which is mandatory. It is used to specify the value of attributes.

$(selector).val(function(index,current_value))

Parameters: This method accepts a single parameter function which is optional. It contains the index position of the element and the current value attribute of the selected element.

Return Value: This method returns the selected element with specified changes made by val() method.

The below examples illustrate the val() method in jQuery:
Example 1: 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>The val Method</title>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
 
    <!-- jQuery code to show the working of this method -->
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                $("input:text").val("GeeksforGeeks!");
                $("input:text").css("color", "green");
                $("input:text").css("font-size", "40px");
                $("input:text").css("font-weight", "bold");
                $("input:text").css("font-family", "times new roman");
            });
        });
    </script>
    <style>
        div {
            background-color: lightgreen;
            padding: 20px;
            width: 41%;
            min-height: 150px;
            border: 2px solid green;
        }
 
        input {
            border: 2px solid green;
            padding-left: 15px;
            width: 350px;
            height: 80px;
        }
    </style>
</head>
 
<body>
    <div>
        <p>
            <input type="text" name="user">
        </p>
        <!-- click on this paragraph and
            see the change -->
        <button>Click Here!</button>
    </div>
</body>
 
</html>
 
 

Output: 

Example 2: This method takes two values first one for index position and the second one for the value of the attribute. No need to pass the index position, the function will automatically be set to null (n = 0), and “c” specifies the current value of the input field. 

jquery-42

html




<!DOCTYPE html>
<html>
 
<head>
    <title>The val Method</title>
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
 
    <!-- jQuery code to show the working of this method -->
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                $("input:text").val(function (n, c) {
                    return c + " GeeksforGeeks!";
                });
            });
        });
    </script>
    <style>
        div {
            background-color: lightgreen;
            padding: 20px;
            width: 300px;
            height: 80px;
            border: 2px solid green;
        }
 
        input {
            border: 2px solid green;
            padding-left: 15px;
            width: 220px;
            font-weight: bold;
            height: 30px;
        }
    </style>
</head>
 
<body>
    <div>
        <p>
            <input type="text"
                   name="user"
                   value="Welcome To">
        </p>
        <!-- click on this paragraph
            and see the change -->
        <button>Click Here!</button>
    </div>
</body>
 
</html>
 
 

Output: 

jquery-43



Next Article
jQuery width() Method

K

kundankumarjha
Improve
Article Tags :
  • JQuery
  • Web Technologies
  • jQuery-Methods

Similar Reads

  • jQuery addClass() Method
    The addClass() method is an inbuilt method in jQuery that is used to add more properties to each selected element. It can also be used to change the property of the selected element. This method can be used in two different ways: 1) By adding a Class name directly: Here, the Class name can be used d
    2 min read
  • jQuery after() Method
    The after() method is an inbuilt function in jQuery which is used to insert content, specified by the parameter for each selected element in the set of matched elements. Syntax: $(selector).after(A);Parameter: It accepts a parameter "A" which is either a content or function passed to the method. Ret
    1 min read
  • jQuery append() Method
    The append() method in jQuery is used to insert some content at the end of the selected elements. Syntax: $(selector).append( content, function(index, html) ) Parameters: This method accepts two parameters as mentioned above and described below: content: It is a required parameter and is used to spe
    2 min read
  • jQuery appendTo() Method
    The appendTo() method is an inbuilt method in jQuery that is used to insert HTML element at the end of the selected element. Syntax: $(content).appendTo(selector)Here the element content specifies the content to be inserted. Parameters: It accepts a parameters "selector" that specifies the elements
    1 min read
  • jQuery attr() Method
    The attr() method in jQuery is used to set or return the attributes and values of the selected elements. Syntax: To return the value of an attribute:$(selector).attr(attribute)To set the attribute and value:$(selector).attr(attribute, value)To set attribute and value using a function:$(selector).att
    3 min read
  • jQuery before() Method
    The before() Method in jQuery is used to add the content before the selected element. Syntax: $(selector).before( content, function(index) )Parameters: This method accepts two parameters as mentioned above and described below: content: This parameter holds the content to be inserted before the eleme
    2 min read
  • jQuery clone() Method
    The clone() method is an inbuilt method in jQuery that is used to make a copy of selected elements including its child nodes, text, and attributes. Syntax: $(selector).clone(true|false)Parameters: This method accepts an optional parameter that could be either true or false specifying whether the eve
    2 min read
  • jQuery css() Method
    The css() method in jQuery is used to change the style property of the selected element. This method can be used in different ways. The css() method can be used to get/return the present value of the property for the selected element. Syntax: $(selector).css(property)or $(selector).css(property, val
    2 min read
  • jQuery detach() Method
    The detach() is an inbuilt method in jQuery that removes the selected elements from the DOM tree including all text and child nodes but it keeps the data and the events. Document Object Model (DOM) is a World Wide Web Consortium standard. This defines accessing elements in the DOM tree. Syntax: $(se
    1 min read
  • jQuery empty() Method
    The empty() method is an inbuilt method in jQuery that is used to remove all child nodes and their content for the selected elements. Syntax: $(selector).empty()Parameter: This method does not accept any parameter. Return Value: This method returns the selected element with the specified changes mad
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences