1

I'm trying to check of all the elements inserted into an array have a class. The code I have so far for the array is this:

var ids = ['#biography', '#videogallery', '#pubblications', '#contacts', '#resume'];
var currentElem = "";

function test(){
    $(ids.join()).each(function() {
        var currentElem = $(this);
    })
};

The problem is that now I'm a bit stuck. I mean, what should I write so that an if statement check if all of the elements with the ids in the array have "test" class?

Something like:

  • if #biography as class "test"
  • if #videogallery as class "test"
  • etc
  • if all of them as the "test" class, then run a function
  • if even one doesn't, then run else.

Hope it's not too confusing. Thanks in advance for any help!

2
  • Why in first place are you using an array and not a jQuery set? Commented Aug 12, 2015 at 10:08
  • use .hasClass method of jQuery. it checks whether element has particular class or not. Otherwise use .is method of jquery Commented Aug 12, 2015 at 10:09

5 Answers 5

5

You can use .filter() to identify the elements with the class, then compare the length property of filtered element and array.

var elementsHavingTestClass = $(ids.join()).filter('.test');    

if(elementsHavingTestClass.length == ids.length){
    //All elements have the class
    //Do something
}else{
    //Do something else
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use filter() function and get elements with the class and Compare the lengths.

elementsWithTest = $(ids.join()).filter('.test');

Comments

0

You can filter the list to only have elements with the 'test' class and then compare the size against the original:

var filtered_arr = $(ids.join()).filter('.test');
if(filtered_arr.length === ids.length) {
    // All have the class
}
else {
    // At least one doesn't have the class
}

Comments

0

This might help

function test() {
    var flag = true;
    $(ids.join()).each(function () {
        if (!$(this).hasClass("yourClass"))
          {
            flag=false;
            break;
          }
    });
    return flag;
}

3 Comments

This will always return true
Now it will return nothing. You should test your code before posting it ;)
Nope... jsfiddle.net/18ub89ek -> "Uncaught SyntaxError: Illegal break statement"
0

It's better if you place the same class to all yours Ids, for example class="landingPageForm", and then you ask if the other class test is present.

// It returns true if it's present, otherwise false
$('.landingPageForm').hasClass('test')

And if you want to do it more stylist you can loop through them verifying if they contain your class test. In my example I used the class error. Below, loginFormAssertion is a function which verify if your inputs contain the class error.

function loginFormAssertion() {
  const fieldsForm = Array.from($(".landingPageForm"));
  return fieldsForm.map(el => { return el.className }).filter(el => el.includes('error'))
}

Now let's simulate you invoke that function when you click on the login button.

 $(".login").click(function() {
    let isThereErrors= loginFormAssertion().length >0
    // You can remove ">0" above and ask in the "if" below in case you want to know exactly the number of times is present the class
    if(isThereErrors) // do this
    else // do this
})

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.