check / uncheck checkbox using jquery?

For jQuery 1.6+ : .attr() is deprecated for properties; use the new .prop() function instead as: For jQuery < 1.6: To check/uncheck a checkbox, use the attribute checked and alter that. With jQuery you can do: Cause you know, in HTML, it would look something like: However, you cannot trust the .attr() method to get the value of the checkbox … Read more

How to find the sum of an array of numbers

Recommended (reduce with default value) Array.prototype.reduce can be used to iterate through the array, adding the current element value to the sum of the previous element values.  Run code snippetExpand snippet Without default value You get a TypeError  Run code snippetExpand snippet Prior to ES6’s arrow functions  Run code snippetExpand snippet Non-number inputs If non-numbers are … Read more

Setting “checked” for a checkbox with jQuery

Modern jQuery Use .prop(): DOM API If you’re working with just one element, you can always just access the underlying HTMLInputElement and modify its .checked property: The benefit to using the .prop() and .attr() methods instead of this is that they will operate on all matched elements. jQuery 1.5.x and below The .prop() method is not available, so you need to use .attr(). Note that this is the … Read more