Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If input contains at least one character

Sorry guys that i am asking a question that has long been answered. But I could not find the answer I needed.

The question is that i want to check value of input. If input after unfocus has one text or more (do something)

 var inputLength = $('input').val();
 if (inputLength.length > 1) {
     $('span.input--madoka').addClass('myclass')
 }
 else  {

 }
like image 791
Dustin Avatar asked Nov 23 '25 22:11

Dustin


2 Answers

Use blur() event to call your function when element loses focus

Here is example

$('#filter').blur(function() {

  var inputLength = $(this).val();

  if (inputLength.length > 1) {

    console.log('has something');

    // here you add whatever class you want to add to other element

  } else {

    console.log('has nothing');

    // here some other action
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="filter" value="" />
like image 189
Akshay Hegde Avatar answered Nov 25 '25 10:11

Akshay Hegde


You can do like this.

var empty = true;
$('#test').on('change', function(){
   if($(this).val()!=""){
      console.log('not empty');
    } else {
      console.log('empty');
    }
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Text : <input type="text" id="test" >
like image 31
Pravin Vavadiya Avatar answered Nov 25 '25 11:11

Pravin Vavadiya