0

I tried many ways to get the selected state of a checkbox. Its a input field with type checkbox.

I tried the following methods to get the state, but all returns false. Only if the checkbox was set selected/not selected with jQuery before (this was done with $.prop("checked", "true").

$("#ID").prop("checked");
$("#ID").val(); //returns "on" everytime
$("#ID").attr("checked");
$("#ID").checked; //undefined

ID is a real id in my Code!

The selected state is also set with jQuery like this:

$(columns[i]).children().first().prop("checked", stateArray[i]);

The checkboxes are in a KendoGrid and columns.children gets all td's and first() returns the input.

stateArray is an array with true/false.

Nothing works... Do you got an idea?

Regards

EDIT: I found the fault... the ID generated for the inputs was always the same... so it selected the first element which was not selected... I'm sorry for that!

Ibrahim Khan
  • 20,280
  • 7
  • 39
  • 53
szmast3r
  • 3
  • 2
  • 4

3 Answers3

2

Where is your selector? Use is(":checked") with it like following.

var status = $(':checkbox').is(":checked");
Ibrahim Khan
  • 20,280
  • 7
  • 39
  • 53
1

To find checkbox checked or not use $('input[type="checkbox"]').prop("checked")

For more understanding check below snippet.

$('input[type="checkbox"]').on('change',function(){
  if($(this).prop('checked')){
    alert("checked");
  }else{
    alert("un-checked");  
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox"/>
Rahul Patel
  • 5,178
  • 2
  • 12
  • 25
  • Do i get a specific Element in a Grid? wont it return every input with type checkbox in my grid? I also need the selected row for a SQLQuery – szmast3r Aug 08 '16 at 07:46
  • Please my check my updated answer. If you have multiple checkbox then in that case you can use "on change" event listener of jquery and $(this) to get the status of checkbox. – Rahul Patel Aug 08 '16 at 09:25
0

You need to select checkbox from DOM first then read its checked property.

$(function() {
  console.log($('#ab3fee6b-c81e-e4ba-b12e-2c088bb386cc_A4').prop('checked'));
  console.log($('#ab3fee6b-c81e-e4ba-b12e-2c088bb386cc_A5').prop('checked'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="ab3fee6b-c81e-e4ba-b12e-2c088bb386cc_A4" type="checkbox" data-role="check" otg-visible="true" style="-webkit-appearance: checkbox !important; width: 20px; height: 20px; display: table-cell; margin: 0px;">
<input id="ab3fee6b-c81e-e4ba-b12e-2c088bb386cc_A5" type="checkbox" data-role="check" checked otg-visible="true" style="-webkit-appearance: checkbox !important; width: 20px; height: 20px; display: table-cell; margin: 0px;">
Mohammad Usman
  • 34,173
  • 19
  • 88
  • 85
  • i tried this already... dont work :/ Changing the selected state with .prop("checked", "true/false") works... but not reading – szmast3r Aug 08 '16 at 07:47