0

I know this might sounds duplicate questions but none of them work for me. So far that i have done is getting the value but i want to know how to get the on/off value from the checkbox. I tried hidden field but i can't get the 1/0 value from the checkbox itself. All i get is the value.

My code is like this

$(document).delegate('.sBorrow', 'change', function(){
            var sBorrowClass = $(this).attr('class');
            var sBorrowValue = $(this).attr('value');
            var sBorrowName = $(this).attr('name');

            if(sBorrowClass.checked = true){
                alert(sBorrowValue);
                alert("unchecked");
            }
            else{
                alert(sBorrowValue);
                alert(sBorrowClass);
            }

For the checkbox form is like this

foreach($r as $row){
            $sBorrow = $_SESSION['sBorrow'];

            echo "<tr align='center'><td>". ($startrow+1) ."</td><td>". $row['matricno'] ."</td><td>". $row['studentname'] ."</td><td>". $row['programme'] ."</td><td>". $row['title'] ."</td><td>". $row['thesis_level'] ."</td><td>". $row['serialno'] ."</td><td>". $row['bavailable'] ."</td><td>
            <form method='post'>
            <input type='checkbox' name='sBorrow' id='sBorrow' class='sBorrow' value='". $row['serialno'] ."'>
            <input type='hidden' name='sBorrow' value=0 />
            </form></td></tr>";
            $startrow++;
            //echo $row['education_level'];

and questions 2.. Also how to make my checkbox remains checked when the user searching for more data after check the checkbox (Livesearch Ajax). Should i use localStorage or SESSION to store the value up until the user refresh?

7
  • = is the assignment operator, not the comparisons which is == or ===. Commented Jul 29, 2016 at 2:39
  • Oh my bad i was hoping its solve it but no. it still goes to else condition even when i checked or unchecked Commented Jul 29, 2016 at 2:40
  • 1
    Oh I didn't notice, but you are getting an attribute, not the element. Commented Jul 29, 2016 at 2:41
  • 1
    Possible duplicate of How to check if a checkbox is checked in jQuery? Commented Jul 29, 2016 at 2:43
  • 2
    Just use $(this). for example $(this)[0].checked or $(this).is(":checked") will work. Commented Jul 29, 2016 at 2:43

1 Answer 1

1

The problem is that attr('string') will return the attribute value as a String which has no property checked. You can use this.checked:

if(this.checked) {
    //Checkbox is checked
}

I hope this will help you.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah it does work. Thanks. Yours and Spencer Wieczorek got the credit for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.