Avatar ·

How to set the "checked" state for a checkbox using jQuery?

Hello everyone! I just started learning jQuery and ran into a problem working with checkboxes. I need to programmatically check and uncheck a checkbox depending on user actions.\n\nI have several checkboxes in a form:\n\n```html\n\n\n\n```\n\nI tried different methods, but they work inconsistently. For example, I use:\n\n```javascript\n$('#agree').attr('checked', true); // seems to work, but not always\n$('.option').attr('checked', 'checked'); // also behaves strangely\n```\n\nBut when I check in the console, I see that the `checked` attribute is added, but the checkbox doesn't change visually. Or vice versa — after unchecking via `.removeAt