Monday, September 3, 2012

How do I check a checkbox with jQuery or JavaScript?


How do I check a checkbox with jQuery or JavaScript?

 
jQuery 1.6+


: Solution : 

Use the new .prop() function:
$(".myCheckbox").prop("checked", true);
$(".myCheckbox").prop("checked", false);


jQuery 1.5 and below

The .prop() function is not available, so you need to use .attr().

To check the checkbox (by setting the value of the checked attribute) do $('.myCheckbox').attr('checked','checked')



and for un-checking (by removing the attribute entirely) do $('.myCheckbox').removeAttr('checked')


Any version of jQuery

If you're working with just one element, it will always be fastest to use DOMElement.checked = true. The benefit to using the .prop() and .attr() functions is that they will operate on all matched elements.


if (this.checked) 
// Assuming an event handler on a checkbox




:D
Steel facing probs ? let me know by comments !

Know More About :
PHP Freelancing India


Reactions:

1 comment:

  1. Our dedicated PHP Programmers have an appropriate mix of expertise and ability that gives you the choice to select most suitable professional/team for your project.

    ReplyDelete

Any Questions or Suggestions ?

About

Professional & Experienced Freelance Developer From India, Technologist, Software Engineer, internet marketer and Open Sources Developer with experience in Finance, Telecoms and the Media. Contact Me for freelancing projects.

Enter your email address:

Delivered by FeedBurner

PHP Freelancer India - Google+