0

I have 2 inputs with same classes:

<input type="text" name="test" class="test">
<input type="text" name="test" class="test" value="test">

One of input is empty second - not. How I can get not empty input value ? Main rule, that input are absolutely same, I cant add some class or something else to define difference.

3 Answers 3

6

You can use .filter() in conjection with Attribute Equals Selector [name=”value”] or Class selector

var nonEmptyInputs = $('input.test[name="test"]').filter(function(){
    return this.value.length > 0;
});
Sign up to request clarification or add additional context in comments.

Comments

1

You can use a loop like

$('button').click(function() {
  var value;
  $('.test').each(function() {
    if (this.value.trim()) {
      value = this.value.trim()
      return false;
    }
  });
  snippet.log(value)
})
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="test" class="test">
<input type="text" name="test" class="test" value="test">

<button>test</button>

Comments

1

try this

    $(function(){
      $('button').click(function() {

        $.each($('.test'),function(){

           if( !$(this).val() )
               $(this).val('this text field was not filled');

        });

      });

    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<input type="text" name="test" class="test">
<input type="text" name="test" class="test">

<button>test</button>

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.