Using addClass and removeClass

Using addClass and removeClass

I have many required textboxes on my form. Each textbox has a label to the left of them. Next to the label, I have a red icon that I want to change to a green icon when its corresponding textbox is changed. The below code is not working. Can someone show me how I would do this?

  1. // CSS
  2. .required
  3. {
  4. background: #ffffff url(dot_red.png) no-repeat 0 0;
  5. padding-left: 20px;
  6. }

  7. .filled
  8. {
  9. background: #ffffff url(dot_grn.png) no-repeat 0 0;
  10. padding-left: 20px;
  11. }
  12. // jQuery
  13. $(function () {
  14. $(".required").change(function () {
  15. if ($(this).val().length > 0) {
  16. $(this).addClass("filled");
  17. }
  18. else {
  19. $(this).removeClass("filled");
  20. }
  21. });
  22. });