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:
How to make a Themed Checkbox using jQuery Mobile ?
Next article icon

How to change the checkbox value using jQuery ?

Last Updated : 03 Aug, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

The Checkboxes are used to let a user select one or more options for a limited number of choices. The :checkbox selector selects input elements with type checkbox.
Syntax: 
 

$('#textboxID').val($("#checkboxID").is(':checked'));


In the above syntax, basically the return value of the checked or unchecked checkbox is being assigned to the textbox. Below examples will illustrate the approach:
Example 1: Here the return value of the checkbox is assigned to the textbox with a function click() whenever the checkbox is clicked to check or uncheck, it assigns the respective return value into the textbox. So, if we want to assign a certain user-defined value in the textbox according to check then we can do that with the use of if/else statement. 
 

html
<!DOCTYPE html> <html>  <head>     <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">     </script>      <script>         $(document).ready(function() {              // Set initial state             $('#textbox2').val($(this).is(':checked'));              // It gets checked to false as uncheck             // is the default             $('#checkbox1').click(function() {                 $('#textbox2').val($(this).is(':checked'));             });         });     </script> </head>  <body>     <center>         <h1 style="color:green;">             GeeksforGeeks         </h1>                   <p>             A Computer Science             Portal for Geeks         </p>                   <input type="checkbox" id="checkbox1" />         Check if true, Uncheck if false.                  <br><br>                  <input type="text" id="textbox2" />     </center> </body>  </html> 

Output: 
 


Example 2: This example contains more than one checkboxes. 
 

javascript
<!DOCTYPE html> <html>  <head>     <script src= "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">     </script>      <script>         $(document).ready(function() {                          // Set initial state             $('#textbox1').val('no');                      // Returns yes or no in textbox1             // when checked and unchecked             $('#checkbox1').click(function() {                 if ($("#checkbox1").is(":checked") == true) {                     $('#textbox1').val('yes');                 } else {                     $('#textbox1').val('no');                 }             });                      // Returns male in textbox2 if checkbox2 checked.             $('#checkbox2').click(function() {                 if ($('#checkbox2').is(":checked") == true) {                     $('#textbox2').val('Male');                 } else {                     $('#textbox2').val('');                 }             });                      // Returns female in textbox2             // if checkbox2 checked.             $('#checkbox3').change(function() {                 if ($('#checkbox3').is(":checked") == true) {                     $('#textbox2').val('Female');                 } else {                     $('#textbox2').val('');                 }             });         });     </script> </head>  <body>     <center>         <h1 style="color:green">             GeeksforGeeks         </h1>                   <p>             A Computer Science             Portal for Geeks         </p>                   <input type="checkbox" id="checkbox1" />         Check If yes!         <br>                  <input type="text" id="textbox1" />         <br>                  <input type="checkbox" id="checkbox2" /> Male         <input type="checkbox" id="checkbox3" /> Female         <br>                  <input type="text" id="textbox2" />     </center> </body>  </html> 

Output: 
 


 

jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it's philosophy of “Write less, do more". You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.


Next Article
How to make a Themed Checkbox using jQuery Mobile ?

T

Tejashwi5
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JQuery
  • jQuery-Misc

Similar Reads

  • How to hide the checkbox based on value in jQuery?
    The checkbox is used to set the content according to the user which he wants to see at the interface. Users also can set multiple choices according to their wishes. Syntax: $('input[name=foo]').attr('checked', false);ApproachIntegrate jQuery in HTML file via CDN Links. Includes the necessary title.
    2 min read
  • How to change the element id using jQuery ?
    The jQuery methods are used to change the element ID which are described below: jQuery attr() Method: This method set/return attributes and values of the selected elements. If this method is used to return the attribute value, it returns the value of first selected element. If this method is used to
    3 min read
  • How to Get the Value of an Input Text Box using jQuery?
    When working with forms or interactive web pages, sometimes need to get the values entered into the input fields. This article provide a complete guide on how to get the value o an input text box using jQuery. Below are the different approaches to get the value of an input text box using jQuery: Get
    2 min read
  • How to make a Themed Checkbox using jQuery Mobile ?
    jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating a Themed Checkbox using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=”stylesheet” hr
    1 min read
  • How to Check a Checkbox with jQuery?
    We can directly select the element by it's selector and then we can use checked property on that for check and unchecked. 1. By using Checked PropertyIn this method, we will select the element using the element selector and directly use the checked property on the item that is present at the passed
    3 min read
  • How to change Selected Value of a Drop-Down List using jQuery?
    We will change the default selected value of the dropdown using jQuery. In an HTML select tag, the default selected value is the value given to the first option tag. With jQuery, it is easy to change the selected value from a drop-down list. Below are the methods to change the selected value of a dr
    3 min read
  • How to Check/Uncheck the checkbox using JavaScript ?
    To check and uncheck the checkbox using JavaScript we can use the onclick event to toggle the checkbox value. We can also check or uncheck all the checkboxes on loading the webpage using JavaScript onload function. In this article, we will learn how to check/uncheck the checkbox using JavaScript. As
    3 min read
  • How to find all checkbox inputs using jQuery ?
    The task is to find all the checkbox input using jQuery. Checkboxes are the square boxes that are ticked when activated, and it is used to select one or more number of choices. Method and Selectors used: :checkbox: This selector is used to select the input element with type = checkbox. .wrap(): This
    2 min read
  • How to make a Disabled Checkbox using jQuery Mobile ?
    jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating a Disabled Checkbox using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=”stylesheet”
    1 min read
  • How to get all selected checkboxes in an array using jQuery ?
    In this article, we are going to discuss various methods to get all the selected checkboxes in an array using jQuery. Several ways of doing this in jQuery are explained in detail with their practical implementation using code examples. Table of Content Using the array.push() method with each() metho
    4 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