jQuery Form How to - Get First selected item value from checkbox collection








Question

We would like to know how to get First selected item value from checkbox collection.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.8.3.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!-- w w  w .  j a  v  a2 s  .c  o m-->
    console.log($("input[type='checkbox']:checked").first().val());
});
</script>
</head>
<body>
  <input type="checkbox" value="1" checked>
  <input type="checkbox" value="2">
  <input type="checkbox" value="3" checked>
</body>
</html>

The code above is rendered as follows: