Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1003531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:59:44+00:00 2026-05-16T07:59:44+00:00

Example html: <div class=red></div> <div class=green></div> <div class=blue></div> <div class=apple></div> I want to loop

  • 0

Example html:

<div class="red"></div>
<div class="green"></div>
<div class="blue"></div>

<div class="apple"></div>

I want to loop through the divs and filter them out if they don’t have a class of either ‘red’, ‘green’, or ‘blue’.

var onlyColorDivs = $('div').hasClass( __________ );

Is there a way to filling the blank in the previous line to accomplish this. Or am I going to have to put my list color classes in an array and do a loop?

Let me know if any clarification is needed.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:59:44+00:00Added an answer on May 16, 2026 at 7:59 am

    All the answers are great and are appropriate. But, if you need a function like this a lot, you could also Monkey Patch the hasClass method to do what you want:

    var _hasClass = $.fn.hasClass;
    $.fn.hasClass = function (classNames) {
      if (!classNames || typeof classNames === "string" ) {
        return _hasClass.call(this, classNames); // Default behavior
      } else {
        // Take array and parse it for the filter method
        var classes = '.' + classNames.join(', .');
        return this.filter(classes).length > 0;
      }
    };
    

    Then you can use it like this:

    $("div").hasClass(['red','green','blue']);
    // or
    $("div").hasClass('green');
    

    Demo on JS Bin

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The example I have in HTML: <div id=red-nav-warp> <ul id=red-nav-logo> <li><img class=sponsors id=sponsorone src=media/img/logosmall.png
I'm using Sass (.scss) for my current project. Following example: HTML <div class=container desc>
Consider the following example: ( live demo here ) HTML: <div class=board> <div class=row>
I have html like so: <div class=foo> (<a href=forum.example.com>forum</a>) <p> Some html here.... </div>
example: Html <div id = parent> <div class = clearDiv></div> </div> css: div.clearDiv {
Consider the following example : HTML: <div class=wrapper> <div class=left>Some text here</div><div class=right>Hello Stack
HTML: <div id=div_id><div class=div_class><p>test</p> </div> <div class=two> <p>asd</p></div> </div> CSS: #div_id p { color:red;
From this code: HTML <div class=test></div> CSS .test { background-color:red; font-size:20px; -custom-data1: value 1;
Given the following HTML example... <div id='div1'>div one</div> <div id='div2'>div two</div> ...I found that
here is link to an example: http://techchorus.net/demos/jquery/hiding-input-elements-in-a-div.html It actally disables few elements on click

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.