0

I have a checkbox. I am trying to use the example from another SO question to figure out if it is selected or not.

However, I only seem to be able to query the state of the element using document.getElementById. I would like to get it working as per the Stack Overflow example. Code below...

<html>
<br>
                <label>Select All BOM's</label>
                <input type="checkbox" id="allboms" name="degbutton"/>
<br>


function doSomething()
    {
        //does not work
        if ($("#allboms").checked){
            //Do stuff

            alert('was checked');
        }

        //does not work
        if ($("#allboms").attr("checked")) {

            //Do stuff
            console.log("boooooooooooo");
            alert('i1 was checked');
        }

        //works
        if(document.getElementById("allboms").checked){
            //Do stuff

            alert('is checked ');
       }else{
            alert('is unchecked .....');
       }
</html>
Community
  • 1
  • 1
user1843591
  • 1,074
  • 3
  • 17
  • 37
  • For that form, it would need to be `$("#allboms")[0].checked` (note the `[0]`). jQuery has no `.checked` property, so you would need to use a jQuery method that let's you check or get it's property value. Also, your code is a total mess and as given would not work at all. – Jared Farrish Sep 06 '14 at 12:38
  • possible duplicate of [jQuery checkbox checked state changed event](http://stackoverflow.com/questions/8423217/jquery-checkbox-checked-state-changed-event) – Robert Sep 06 '14 at 12:38
  • the answer you accepted was not the first one..my answer was few seconds ahead of stephen's answer .. thanks.. – Kartikeya Khosla Sep 06 '14 at 13:31
  • Hi @Exception, According to the SO list Stephen is first. If i select 'oldest' tab it orders his as first. If you can show you were first I will gladly change. I am only trying to be fair and I do appreciate everyone who replied... – user1843591 Sep 06 '14 at 19:29
  • @Exception, When I submitted my answer, yours was not visible. If OP wishes to accept your answer that's fine by me (I'm here to help people, not for personal glory) although if anything, its Jared's answer that should be accepted because its the most comprehensive. –  Sep 07 '14 at 05:02
  • Even i m also here to help others not for personal glory..i m not saying that questioner accept my answer... i m just clarifying here is that questioner is saying that my answer was not the first one... okk no worries... my answer was the first one and you also know that... i will contact stackoverflow...thanks.. @StephenMuecke – Kartikeya Khosla Sep 07 '14 at 05:58
  • Well...Yes..you can just see actual timings by just hover over the timestamp which states the exact post timings..you can just see exact timings my answer was 13 seconds ahead of Stephen's answer(exact timings my answer timings is 12.37.12 and Stephen answer timing is 12.37.25) and I m clearing here that I m not saying that accept my answer as it was first correct answer I m just giving explanation of @user1843591(questioner) comment that Stephen's answer is the oldest..thanks and glad that your problem solved... – Kartikeya Khosla Sep 07 '14 at 08:04
  • Please note that the "accepted" checkmark is intended to be given at the asker's discretion, to whichever answer the asker personally considered most helpful. The asker is welcome to consider posting timestamps, but it's by no means a requirement. Please do not pressure people about this sort of thing. @Kartikeya – Pops Sep 10 '14 at 15:47
  • @Pops....i m not forcing anyone to accept.. plz read above comments carefully.. i m just clarifying one the comment of questioner that stephen answer is the oldest... thanks.. – Kartikeya Khosla Sep 10 '14 at 16:23

3 Answers3

2

These are all the forms I can think of using plain Javascript and jQuery:

$(document).ready(function rd(){
    var $allboms = $('#allboms');

    $allboms.on('change', function ch(){
        if (this.checked) {
            console.log('this.checked is true.');
        }

        if ($(this)[0].checked) {
            console.log('$(this)[0].checked is true.');
        }

        if ($(this).prop('checked')) {
            console.log('$(this).prop("checked") is true.');
        }

        if ($(this).is(':checked')) {
            console.log('$(this).is(":checked") is true.');
        }
    });
});

http://jsfiddle.net/dyeu7w77/

Jared Farrish
  • 48,585
  • 17
  • 95
  • 104
1

Try This :-

 if ($("#allboms").is(':checked')){
   .....
 }
Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69
1

Try

if ($("#allboms").is(':checked')) {
  ..
}