0

i'm new in jquery and i dont know how to use it, this is code i create look like

<div id="checkbox-container">
    <div class="hidefull">
        <input type="checkbox" value="hideFull" id="check-hide-full-table">Hide Full
    </div>
    <div class="hideempty">
        <input type="checkbox" value="hideEmpty" id="check-hide-empty-table">Hide Empty
        </div>
</div>

and jquery i create like this

function test(){
    alert("test called");
    $("#check-hide-empty-table").prop("checked", true);
}

so the problem is, how to checked checkbox using jquery? i'm already look this but still not checked. somebody know what's wrong with my code?

i want create if load new page, that checkbox, checked.

FYI, that alert showed.

Community
  • 1
  • 1
Pentolan
  • 161
  • 11
  • 4
    You have an extra `,` in your `prop("checked", true)`. Remove it and this should work fine. – Tyler Roper Nov 25 '16 at 02:49
  • ah sry, my code is not have extra `,` but still not working.i just typo, i'm already edit my question, – Pentolan Nov 25 '16 at 02:54
  • when are you calling your `test` function? – jkris Nov 25 '16 at 02:56
  • I've [**copy & pasted**](https://jsfiddle.net/41n1aat3/) your code exactly into a JSFiddle and it seems to be working fine. Are you sure you're including jQuery? Are you getting any errors when you look in console? Are you calling `test();` in a `$(document).ready(...)` to ensure that the element exists when you try to check it? – Tyler Roper Nov 25 '16 at 02:56
  • Possible duplicate of [Setting "checked" for a checkbox with jQuery?](http://stackoverflow.com/questions/426258/setting-checked-for-a-checkbox-with-jquery) – Sreekanth Nov 25 '16 at 02:58
  • ah thanks to make me sure about my code is correct @Santi – Pentolan Nov 25 '16 at 03:04
  • yeah i figure it out, is my mistake wrongly called test function @jkris, i called test function before i load my checkbox – Pentolan Nov 25 '16 at 03:05

2 Answers2

0
jQuery(document).ready(function() {

    test("#check-hide-empty-table");

});

function test(input){

    alert("test called");

    testInput = jQuery(input);
    testInput.click(function(){
        if(this.checked != true) {

            alert('not checked');
            return false;
        }

        alert('checked');
        return true;

    });

}
jeremykenedy
  • 4,150
  • 1
  • 17
  • 25
-1

Here is the solution: Demo

$(document).ready(function() {
   $('input').on('change', function() {
      if ($(this).attr("checked")) {
          $(this).attr("checked", false);
      } else {
          $(this).attr("checked", true);
      }
   });
});

When checkbox is checked it, It add attribute checked and if unchecked it take out the check

Luminous_Dev
  • 614
  • 6
  • 14