0

I want to check a checkbox is checked or not using jquery. When click on the check box, check the checkbox is checked or not using jquery. My code is given below.

   <script>
   function check(id)
    {
     if($("#id:checked").length == 1)
      {
        alert("cheked");
      }
    }
   </script>


     Html
     $i=1;
     <input name="" type="checkbox" value="" 
     id="one<?php echo $i; ?>"  onclick="check(one<?php echo $i; ?>)"/>
Ivan Gerasimenko
  • 2,381
  • 3
  • 30
  • 46
vani kk
  • 5
  • 3
  • possible duplicate of [Checking a checkbox with jQuery?](http://stackoverflow.com/questions/426258/checking-a-checkbox-with-jquery) – usr1234567 Feb 26 '15 at 05:32
  • possible duplicate of [Check checkbox checked property](http://stackoverflow.com/questions/901712/check-checkbox-checked-property) – gapple Feb 26 '15 at 05:58

1 Answers1

0

Pass the element reference to the method

<input name="" type="checkbox" value="" id="one<?php echo $i; ?>"  onclick="check(this)"/>

then check the checked property value

function check(el) {
    if (el.checked) {
        alert("cheked");
    }
}

Note: Since you are using jQuery, instead of using inline event handlers try to use jQuery event handlers


In your script the problem is, id is a variable holding the actual id so you need to use string concatenation

function check(id) {
    if ($("#" + id + ":checked").length == 1) {
        alert("cheked");
    }
}

Also since the id is a string, it has to be enclosed in a '' in the click handler call

<input name="" type="checkbox" value="" id="one<?php echo $i; ?>"  onclick="check('one<?php echo $i; ?>')"/>
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531