Remove class with jQuery according to user behavior -
i have textbox
<input type="text" name="txt1" id="txt1" class="comment">
that adds class of '.error' when dropdown option selected:
.error { color:red; }
but trying remove '.error' class when user begins type in textbox, like:
if ($('.comment').val().length > 0) { removeclass('.error'); }
apart minor syntax errors, main question when want code called. check on every keystroke, use keyup()
. , if check value
, removeclass()
on this
(where that's specific element key typed in), can have multiple .comment
fields same behavior.
$('.comment').keyup( function() { if (this.value.length > 0) { $(this).removeclass('error'); } } )
.error { background-color: red; // changed easier demo visibility }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input type="text" name="txt1" id="txt1" class="comment error"> <input type="text" name="txt2" id="txt2" class="comment error">
Comments
Post a Comment