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

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -