Try It Editor
Tip: Press
Ctrl/⌘ + Enter
to Run
▶ Run
Reset
Copy
Download
Code
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>attr() vs prop()</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> </head> <body> <label><input id="chk" type="checkbox"> Subscribe</label> <p> <button id="read">Read</button> <button id="setAttr">attr('checked', 'checked')</button> <button id="setProp">prop('checked', true)</button> </p> <pre id="out"></pre> <script> $("#read").on("click", function(){ $("#out").text("attr: " + $("#chk").attr("checked") + " | prop: " + $("#chk").prop("checked")); }); $("#setAttr").on("click", function(){ $("#chk").attr("checked", "checked"); }); $("#setProp").on("click", function(){ $("#chk").prop("checked", true); }); </script> </body> </html>
Output