0

I´m looking for a solution,to non empty inputs and textareas add aditional class.

I have a solution, which work perfect for textares, but not for inputs:

<input type="text" class="move" value="something"> 

<textarea type="text" class="move">something</textarea>

$(document).ready(function() {

    $('.move:not(:empty)').addClass('used');

}); 

Any ideas? Thanks!

2
  • Where is the class "used"? Not sure what you want to display in your boxes, All of them have "something" displayed at the moment Commented Jul 27, 2015 at 23:59
  • the class used is added after reaload of the page - this is only example Commented Jul 28, 2015 at 0:01

1 Answer 1

2

with CSS pseudo selectors, look what happen:

$(function() {

    $('.move:empty').addClass('used');

});
.used {
  background:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="move" value="something">
<input type="text" class="move" value="">
<textarea type="text" class="move">something</textarea>

retrieving input's value

$(document).ready(function() {

    $('.move').each(function(e,i){
      if(this.value !== "") $(this).addClass('used');
    });

}); 
.used {
  background:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea type="text" class="move">something</textarea>
<input type="text" class="move" value="something">
<input type="text" class="move" value="">

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.