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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:39:33+00:00 2026-06-03T18:39:33+00:00

Given the following html: <div id=’foo’> <div class=’prodbar’> <div class=’title’>Group 1</div> <div class=’product’>Product #1</div>

  • 0

Given the following html:

<div id='foo'>
  <div class='prodbar'>
    <div class='title'>Group 1</div>
    <div class='product'>Product #1</div>
    <div class='product'>Product #2</div>
    <div class='product'>Product #3</div>
    <div class='product'>Product #4</div>
  </div>
  <div class='prodbar'>
    <div class='title'>Group 2</div>
    <div class='product'>Product #5</div>
  </div>
  <div class='prodbar'>
    <div class='title'>Group 3</div>
    <div class='product'>Product #6</div>
    <div class='product'>Product #7</div>
  </div>
</div>

I have code that adds the class 'selected' to any product that is clicked. After some products are selected, the user can click a button which removes the selected products from the screen, which is simply:

$(".selected").remove();

Now, if that empties a product group, I’d like the div surrounding the group div to be removed as well. So in the above example, if Product #5 is selected and removed, I want the prodbar that now has nothing but a title to be removed with it.

I know I can accomplish what I want with a loop, but I was hoping for something more jQuery-elegant. Essentially, I need to know if a div has only 1 child. I’m fairly new to jQuery and I’m not sure if this is doable directly in the selectors. Any ideas?

  • 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-03T18:39:34+00:00Added an answer on June 3, 2026 at 6:39 pm

    If you looking for a clean up code then below will filter all prodbar and removes if title is the only children,

    DEMO

    $('button').click(function() {
    
        $('.selected').parent().filter(function() {
           if ($(this).children().not('.selected').length <= 1) { //remove the prodbar
              return true;
           } else { //remove selected alone
              $(this).find('.selected').remove();
              return false;
           }
        }).remove();
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following html code (in the given order) <div id=content>...</div> <div id=footer>...</div>
Given the following html... <div id=group> <ul> <li><div class=category>one</div></li> <li><div class=category>one</div></li> <li><div class=category>onetwo</div></li> <li><div
Using jQuery 1.6.1 , given I have the following HTML: <div class=control> <label>My Control</label>
Given the following code: HTML: <div id=nav_bar> <ul> <li ><a href=# class=current>HOME</a></li> <li><a href=#>GALLERY</a></li>
Given the following HTML example... <div id='div1'>div one</div> <div id='div2'>div two</div> ...I found that
Newbie question. Given the following html. <div id=mycontainer1 class=container> <input type=text class=name/> <input type=text
Given the following HTML fragment: <div class=word> <input type=text name=A /> <input type=text name=n
Given the following HTML structure: <div class=wrap> <div id=a></div> <div id=b></div> </div> the following
Given the following html: <div id=f52_lblQuestionWording title= style=width:auto;height:auto; display: inline; overflow: hidden; >Home telephone</div>
I have been given the following: <div class=elf>Label 1</div> <div class=eff>Data 1</div> <div class=elf>Label

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.