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 8845947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:51:23+00:00 2026-06-14T11:51:23+00:00

I have this markup: <div class=click>click</div> <div id=wrapper> <div class=container> <div data-attrfirst=’type1′><span data-attrsecond=’type2′>must be

  • 0

I have this markup:

<div class="click">click</div>
<div id="wrapper">
<div class="container">
<div data-attrfirst='type1'><span data-attrsecond='type2'>must be red</span></div>
</div>
</div>

I want to select all the .containers that:
1. are inside of #wrapper;
2. contain data-attrfirst='type1' element which, in its turn, contain data-attrsecond='type2' element.
My requirements:
1. My ‘#wrapper’, ‘type1’, ‘type2’ values MUST be kept as variables.
2. I MUST find this all inside of some tag (in this case, I choose body).

I tried this (asterisks may be removed, no difference):

<script>
    $(document).ready(function () {
        $('.click').click(function () {
            var myid = '#wrapper';
            var attrfirst = 'type1';
            var attrsecond = 'type2';
            var elem = $('*[data-attrfirst=' + attrfirst + ']').has('*[data-attrsecond=' + attrsecond + ']');
            $('body').find(myid + ' .container').each(function (i) {
                if ($(this).has('*[data-attrfirst=' + attrfirst + '] *[data-attrsecond=' + attrsecond + ']')) {
                    $(this).css('color', 'red')
                }
            });

            $('body').find(myid + ' .container').each(function (i) {
                if ($(this).has(elem)) {
                    $(this).css('color', 'red')
                }
            });
            $('body').find(myid + ' .container').filter(function (i) {
                return $('*[data-attrfirst=' + attrfirst + '] *[data-attrsecond=' + attrsecond + ']', this).length >= 1
            }).css('color', 'red')
        });
    });
</script>

Where am I wrong? And I don’t understand from Jquery docs, does .has accept Jquery object?

  • 1 1 Answer
  • 0 Views
  • 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-06-14T11:51:25+00:00Added an answer on June 14, 2026 at 11:51 am

    Your question says that you want to find the #container, so use this:

    $('body #wrapper .container:has([data-attrfirst="type1"] [data-attrsecond="type2"])');
    

    Or, to be a little less confusing, you can work backwards:

    $('[data-attrfirst="type1"] [data-attrsecond="type2"]')
         .closest('body #wrapper .container');
    

    However, from your code it seems that you actually want to find that last data-attrsecond="type2" element. In that case, use this:

    $('body #wrapper .container [data-attrfirst="type1"] [data-attrsecond="type2"]');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have this html markup: <div id=wrapper> <pre class=highlight> $(function(){ // hide all
I have markup similar to this: <div class='wrapper plugin'> //some content <div class='child-wrapper plugin'>
I have this markup <div class=input text> <label for=Experience0ExperienceContent>Experience Content</label> <input name=data[Experience][0][experience_content] class=span3 id=Experience0ExperienceContent
I have the following markup: <div class=event-row-container> <div data-id=1 class=ios-location-name>Location Name</div> <div data-id=1 class=ios-location-detail
I have this markup: <div id=slider1> <div class=ui-slider ui-slider-horizontal ui-widget ui-widget-content ui-corner-all> <a style=left:
I have a markup like this: <div class=sentence> <ul title=list 3> <li> <ul title=list
I have the following markup; <div class=model-timeline> <p>Text</p> <div class=timeline> <div class=span-blank-5></div> <div class=time-span-1></div>
I have this markup: <div class=news-listItem> <p class=date>Date: 06.03.2012</p> <h2><a href=#>test aaaaa</a></h2> <div class=news-content>Lorem
I have this markup: <span id=tabPhp class=infoData>PHP</span> <span id=tabJquery class=infoData>jQuery</span> <span id=tabWordpress class=infoData>WordPress</span> <span
In my CakePHP application, I have a view with this markup: <div class=copy id=share-copy></div>

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.