John
John

Reputation: 29

how to alert the value of checkbox when it's checked

I want to alert the value of a specific input[checkbox] when an input[checkbox] is checked. I have many input checkbox. so when it's checked, alert will fire.

<input type="checkbox" value="1">
<input type="checkbox" value="2">
<input type="checkbox" value="3">
....

Upvotes: 1

Views: 5205

Answers (1)

Jayesh Chitroda
Jayesh Chitroda

Reputation: 5049

Try this:

$('input[type="checkbox"]').change(function() {
  if ($(this).is(":checked")) {
    alert($(this).val())
    return;
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" value="1">
<input type="checkbox" value="2">
<input type="checkbox" value="3">

Upvotes: 2

Related Questions