1

I have two divs containing the same checkbox with the same name,class and id. When I am selecting the checkbox of a particular div , it should alert the value of that checkbox when submitting a button.

Example :

<div>

    <input type="checkbox" class="snowplay" id="snowplayy" name="snowplay" value="1">
</div>
<div>

    <input type="checkbox" class="snowplay" id="snowplayy" name="snowplay" value="2">
</div>
<div>

    <input type="submit" class="submit" id="submit" name="submit" />
</div>

When I am clicking any of the two checkboxes, and then submitting the button, I want to alert the value of the checkbox of that particular checkbox. Can anyone say how to do this ?

1
  • 2
    ID should be unique Commented Oct 3, 2016 at 9:01

2 Answers 2

1

$('.submit').click(function(e) {
  e.preventDefault()
    //alert($('.snowplay:checked').val()) //single value
  $('.snowplay:checked').each(function() {//iterate over 
    alert($(this).val()) // many value

  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>

  <input type="checkbox" class="snowplay" id="snowplayy" name="snowplay" value="1">
</div>
<div>

  <input type="checkbox" class="snowplay" id="snowplayy1" name="snowplay" value="2">

</div>

<div>

  <input type="submit" class="submit" id="submit" name="submit" />

</div>

  1. Use this .snowplay:checked checkbox with class snowplay that is checked
  2. Change your id to be unique
Sign up to request clarification or add additional context in comments.

Comments

0

Id should be unique. So here i removed the id Attribute :).

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#submit').click(function(){
$('.snowplay:checked').each(function(){
alert($(this).val())
});

});

});
</script>
</head>
<body>
<div>

    <input type="checkbox" class="snowplay"  name="snowplay" value="1">
</div>
<div>

    <input type="checkbox" class="snowplay"  name="snowplay" value="2">
</div>
<div>

    <input type="submit" class="submit" id="submit" name="submit" />
</div>

</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.