jQuery – get checkbox value and checked state

jQuery can be used to access checkbox value using jQuery.attr(“value”) and it checked state using jQuery.prop("checked"). Note that we can not use jQuery.attr() for checked state. Here is example code snippet.

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>

<input id="cb1" type="checkbox" value="val1" checked>chekbox1
<pre id="msg"></pre>

<script>
function display_cb_info() {
  var value = $("#cb1").attr("value");
  var isChecked = $("#cb1").prop("checked");
  $("#msg").text("value=" + value + " isChecked=" + isChecked + "\n");
}
$(function() {
  display_cb_info();
  $("#cb1").change(display_cb_info);
});
</script>
refresh

Related

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments