-3

There are three check boxes . i want to get response as true when corresponding checkbox will be checked and false if unchecked .

<div class="">
  <p>
    <input type="checkbox" id="indeterminate-checkboxm1 stream_id1" />
    <label for="indeterminate-checkboxm1" class="black-text">B.Tech/B.E.</label>
  </p>
  <p>
    <input type="checkbox" id="indeterminate-checkboxm2 stream_id2" />
    <label for="indeterminate-checkboxm2" class="black-text">Diploma</label>
  </p>
  <p>
    <input type="checkbox" id="indeterminate-checkboxm3 stream_id3" />
    <label for="indeterminate-checkboxm3" class="black-text">B.Sc</label>
  </p>
</div>
Tushar Walzade
  • 3,737
  • 4
  • 33
  • 56
  • 4
    Possible duplicate of [Check if checkbox is checked with jQuery](https://stackoverflow.com/questions/2204250/check-if-checkbox-is-checked-with-jquery) – Serge K. Jun 29 '17 at 06:51
  • `$("#indeterminate-checkboxm2 stream_id2":checked")` or `$("#indeterminate-checkboxm2 stream_id2").is(":checked")` – Carsten Løvbo Andersen Jun 29 '17 at 06:51

3 Answers3

1

You need to associate a click event on checkbox and check if it is checked or not like this

$('input[type="checkbox"]').click(function() {
    alert($(this).is(':checked'));
});

Here is the link to PLUNKR for your work around

Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
0

Here is the working fiddle for your code.

There should be a unique id for each checkbox. So, either it'll be indeterminate-checkboxm1 or stream_id1, but you can't use both of them at a time.

So, your html will be -

<div class="">
        <p>
            <input type="checkbox" id="indeterminate-checkboxm1" />
            <label for="indeterminate-checkboxm1" class="black-text">B.Tech/B.E.</label>
        </p>
        <p>
            <input type="checkbox" id="indeterminate-checkboxm2" />
            <label for="indeterminate-checkboxm2" class="black-text">Diploma</label>
        </p>
        <p>
            <input type="checkbox" id="indeterminate-checkboxm3" />
            <label for="indeterminate-checkboxm3" class="black-text">B.Sc</label>
        </p>
    </div>

And the generic Jquery code will be like this -

$('input[type="checkbox"]').click(function() {
    var id = $(this).prop('id');
    $(this).is(':checked') ? alert('checked ' + id) : alert('unchecked ' + id);
});
Community
  • 1
  • 1
Tushar Walzade
  • 3,737
  • 4
  • 33
  • 56
0

$(function(){
$('input[type=checkbox]').each(function(){
if($(this).prop('checked')){
console.log('true');
}
else{
console.log('false');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="">
                    <p>
                        <input type="checkbox" id="indeterminate-checkboxm1 stream_id1"/>
                        <label for="indeterminate-checkboxm1" class="black-text">B.Tech/B.E.</label>
                    </p>
                    <p>
                        <input type="checkbox" id="indeterminate-checkboxm2 stream_id2"/>
 <label for="indeterminate-checkboxm2"
  class="black-text">Diploma</label>
                    </p>
                    <p>
   <input type="checkbox" id="indeterminate-checkboxm3 stream_id3"/>
                        <label for="indeterminate-checkboxm3"
                               class="black-text">B.Sc</label>
                    </p> 
                    </div>
lalithkumar
  • 3,480
  • 4
  • 24
  • 40