I'm not exactly sure what's going here, but at first glance, this is what I see:
I think you are using attr() wrong. In the first section of code, you are checking the 'Protected' attribute, but in the second block of code, you are setting an 'onchange' attribute to the value of 'Protected', Maybe this should read attr('Protected', true) instead?