Question

Basically I have a form that displays a bunch of dynamic checkboxes from mysql database - this works fine. I have an attribute called "default" that I put the value of the mysql entry into so later I can determine if the user changes the checkbox value from what was originally displayed. Here's part of the while statement that generates these checkboxes:

printf("\t<tr class='recordrow'>
        \n\t\t<td><a class='page_modal' href='#mWindow%s' target='_blank'>%s</a></td>
        \n\t\t<td class='ppath'>%s</a></td>
        \n\t\t<td>%s</td>
        \n\t\t<td><input type='checkbox' class='checkbox' name='$page_path current' default='$currentcheckbox' $currentcheckbox/><div class='page_dialog' id='mWindow%s'><img src='exit.png' class='close' alt='close'/><img src='newwin.jpg' alt='new window'/><div id='content_section'>%s</div></div> </td>
        \n\t\t<td><input type='checkbox' class='checkbox' name='$page_path prospective' default='$prospectivecheckbox' $prospectivecheckbox/></td>
        \n\t\t<td><input type='checkbox' class='checkbox' name='$page_path faculty' default='$facultycheckbox' $facultycheckbox/></td>
        \n\t\t<td><input type='checkbox' class='checkbox' name='$page_path staff' default='$staffcheckbox' $staffcheckbox/></td>
        \n\t\t<td><input type='checkbox' class='checkbox' name='$page_path external' default='$externalcheckbox' $externalcheckbox/></td>

Now I have a little JQuery script that I think should do what I want it to do, but something appears to be wrong in my logic or something.... Basically when a user makes a change to a checkbox the Jquery script is triggered and it pops up with an alert saying if a checkbox is being checked or unchecked. Then I have an if statement that should see if the checked state of the checkbox is different from the original value in my default attribute. What am I doing wrong here? I want to see my alert "value is different from original" if the user changes the checkbox checked/unchecked state.

$(document).ready(function() {
$('.checkbox').change(function() {
    if($(this).is(":checked")) {
        var returnVal = confirm("checked");
        if($(this).attr("default").val() != returnVal)
        {
            alert("Value is different from original");
            //put element in changed item array
        }
    }
    else{
        var returnVal = confirm("unchecked");
        var returnVal = "0";
        if($(this).attr("default").val() != returnVal)
        {
            alert("Value is different from original");
            //put element in changed item array
        }
    }
});

I get this error in my dev tool for chrome when I check and uncheck checkboxes: Uncaught TypeError: Object 0 has no method 'val'

Was it helpful?

Solution

$(this).attr("default") returns a string containing the value of the attribute. It should be sufficient to use this alone without calling .val().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top