0

I have already research about this but. I have two different class to check. How can I do that? Please visit this link. Sir @Arun P Johny had the answer. But What if I have two or more class to check?

Like so...
<input type="text" class="myClass[]">
<input type="text" class="myClass[]">
<input type="text" class="myClass[]">
<input type="text" class="myClass2[]">
<input type="text" class="myClass2[]">

1
  • myClass[] array of class is not required you can just put myClass this work Commented Jun 27, 2017 at 7:47

4 Answers 4

2

Use Multiple Selector (“selector1, selector2, selectorN”), Also as you are using meta-characters in the CSS class you have to escape them with \\

var elements = $('.myClass\\[\\],.myClass2\\[\\]').filter(function() {
    return this.value != ''
});

$('button').click(function() {
  var elements = $('.myClass\\[\\],.myClass2\\[\\]').filter(function() {
    return this.value != ''
  });

  if (elements.length == 0) {
    alert('empty')
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="myClass[]">
<input type="text" class="myClass[]">
<input type="text" class="myClass[]">
<input type="text" class="myClass2[]">
<input type="text" class="myClass2[]">
<button type="button">test</button>

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

2 Comments

Can you provide a snippet or a jsFiddle?
Is this available for <select> elements?
1

You can either use class^=myClass as in the example below, or you can use $(".myClass\\[\\], .myClass2\\[\\]")

class^=myClass means that your class has to start with myClass

$('button').click(function() {
  var flag = false;
  $('input[class^=myClass]').filter(function() {
    if (this.value != '') {
      flag = true;
      //no need to iterate further
      return false;
    }
  });

  if (!flag) {
    alert('empty')
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="myClass[]">
<input type="text" class="myClass[]">
<input type="text" class="myClass[]">
<input type="text" class="myClass2[]">
<input type="text" class="myClass2[]">
<button>test</button>

2 Comments

What if I change the classes? like 3 class="exam[]", 2 class="total_exam[]", 4class="final_exam[]"
Then i would do $('input[class*=exam\[\]]')
0

Try this

if($( "input[class*='myClass']" ).val()==""){

}

Comments

0

If your input element contains only one class name as myClass[] or myClass2[] then you can use this

if($('input[class^="myClass"]').val()==""){}

But if you have some other classes for input like

<input type="text" class="testClass myClass[]">

then you need to use this

if($('input[class*="myClass"]').val()==""){}

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.