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?
- // CSS
- .required
- {
- background: #ffffff url(dot_red.png) no-repeat 0 0;
- padding-left: 20px;
- }
- .filled
- {
- background: #ffffff url(dot_grn.png) no-repeat 0 0;
- padding-left: 20px;
- }
- // jQuery
- $(function () {
- $(".required").change(function () {
- if ($(this).val().length > 0) {
- $(this).addClass("filled");
- }
- else {
- $(this).removeClass("filled");
- }
- });
- });