0

I am trying to determine if a checkbox is checked with jQuery multi page setup, but it always returns "false" unless I mark the item as "checked" per default.

My code looks pretty basic to me:

  <script>
    function updateUrls(){
      console.log('test: ' + $('#filter_pic').is(':checked'));
    });
  </script>
  <form>
    <input type="checkbox" name="filter_pic" id="filter_pic" onchange="updateUrls();">with pix
  </form>

The reason for this might be, that this code is inside a multi page setup. In fact the code looks similar to this:

   <div data-role="page" id="select_dienstleistungen">  
        <div data-role="content"> 
        <script>
        function test() {
          console.log('test: ' + $('#filter_pic').is(':checked'));
        };
   ....
        <form>
           <input type="checkbox" name="filter_pic" id="filter_pic" onchange="updateUrls();">with pix
        </form>
   </div
   <div data-role="page" id="select_jobs">  
        <div data-role="content"> 
        <script>
        function test() { 
    ...      

How is it possible to address the item inside the pages? All those pages have the same checkbox item as the code is valid for all those different pages.

merlin
  • 2,717
  • 3
  • 29
  • 59

2 Answers2

1
<script type="javascript/text">
    $('#filter_pic').change(function() {
        console.log('test: ' + $(this).prop('checked'));
    });
</script>
<form>
    <input type="checkbox" name="filter_pic" id="filter_pic" />with pix
</form>

Took out the "onchange".

Original Post

Use

$('#filter_pic').prop('checked')

instead to check the property.

Also, I think it may be a JQuery version problem. Other people have had this problem as well even though the "is(':checked')" works elsewhere.

James
  • 3,765
  • 4
  • 48
  • 79
0
<script>
    function updateUrls(){
      console.log('test: ' + $('#filter_pic').is(':checked'));
    };
  </script>
  <form>
    <input type="checkbox" name="filter_pic" id="filter_pic" onchange="updateUrls();">with pix
  </form>

// One extra parenthesis ')' before the ';' semicolon. I have checked by removing this one. And is working fine in my end.

Ahmad Sharif
  • 4,141
  • 5
  • 37
  • 49