jQuery check and uncheck a checkbox

August 2nd, 2012 by Laeeq | 2 comments

In my previous post, you had learned how to set and get form element values using JQuery.

As we know that jQuery is a Javascript framework which simplify Javascript coding complexity and also fixed cross browser issues. In this post, you will learn how to check and uncheck checkbox using JQuery.

Example html form

  1. <form>
  2. <input type=“checkbox” name=“city” id=“city” value=“Newyork” />
  3. </form>

check and uncheck checkbox using JQuery

There are several ways to check and uncheck checkbox with jQuery. The element attribute will return true if it’s checked or false if it’s not.

Below is the example code of JQuery to check a checkbox:

  1. $(‘#city’).attr(‘checked’, true);

Use below code  to if you have not set element id

  1. $(‘input[name=city]‘).attr(‘checked’, true);

and like so to uncheck the checkbox:

  1. $(‘#city’).attr(‘checked’, false);

or

  1. $(‘input[name=city]‘).attr(‘checked’, false);

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

 

 

  1. August 3rd, 2012 at 07:57 | #1

    I have read your blog and i got a very useful and knowledgeable information from your blog.its really a very nice article.You have done a great job .
    Thank you so much for sharing.

  2. camiladavisei
    October 3rd, 2012 at 14:37 | #2

    I truly enjoy examining on this website , it contains superb blog posts. “The secret of eternal youth is arrested development.” by Alice Roosevelt Longworth.

  1. No trackbacks yet.