jQuery addClass example

Change class name on click in jQuery

by Павел KLON

HTML

<input type="text" class="hours">

CSS

.inputError {
    color: red;
}

JavaScript

$(".hours").bind("keyup change", function (e) {
        var tval = $(this);
        if (!($.isNumeric(tval.val() ) || tval.val() == 'нет'  ) ) {
          tval.addClass("inputError");
				}else{
        if (tval.val() === "") {
            tval.prop('required', true);
        } else if (tval.val() >= 24) {
            tval.val("24");
            tval.addClass("inputError");
        } else {
            tval.removeClass("inputError");
            tval.prop('required', false);
        }
        }
        
    });