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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:31:17+00:00 2026-06-02T04:31:17+00:00

I have a group of buttons, which I’m adding buttons to dynamically. My selection

  • 0

I have a group of buttons, which I’m adding buttons to dynamically. My selection will look something like this:

$elements = [a.midToggle, a.menuToggle, a.ui-crumbs]

I want to prepend this selection to an existing controlgroup:

<div data-role="controlgroup" data-type="horizontal" class="dropZone">
   <a href="#" class="some">Some</a>
   <a href="#" class="midToggle">MidTog</a>
</div>

However before prepending, I want to remove the buttons already inside the controlgroup from my selection, because otherwise they will be in there multiple times.

I’m trying like this, but it doesn’t work at all:

// I have multiple controlgroups, so I need to add the buttons to all of them
$('.dropZone').each(function() {     

   var $first = $(this), 
   $buttons = $elements.clone();

   $buttons.each(function() {
     // check if class name on new button is already in controlgroup
     if ( $(this).is(".midToggle") && $first.find(".midToggle").length > 0 ) {
     $(this).remove();
         }
      if ( $(this).is(".menuToggle") && $first.find(".menuToggle").length > 0 ) {
     $(this).remove();
         }
      if ( $(this).is(".ui-crumbs") && $first.find(".ui-crumbs").length > 0 ) {
     $(this).remove();
         }
      });
 // append what's left
 $first.append( $buttons ) 

I figure my $buttons aren’t removed, but I don’t know how to get it to work. Also my three if-statements are kind of lame. Is there a better way to do this?

EDIT:
I had to modifiy the solution a little, because each button has multiple classes, so I cannot simply check for attr(‘class’). This is not perfect, but works:

function clearOut($what) {
    $buttons.each(function () {
        if ($(this).is($what)) {
            $buttons = $buttons.not($what)
            }
        });
     }

// filter for existing buttons
// TODO: improve
if ($first.find('.midToggle')) {
    clearOut('.midToggle');
    }
if ($first.find('.menuToggle')) {
    clearOut('.menuToggle');
    }
if ($first.find('.ui-crumbs')) {
    clearOut('.ui-crumbs');
    }
  • 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-02T04:31:18+00:00Added an answer on June 2, 2026 at 4:31 am

    I shrieked your code into half:

    $('.dropZone').each(function() {
        var $dropZone = $(this);
        var $buttons = $elements.clone();
        $buttons.each(function() {
            var $button = $(this);
    
            if ($dropZone.find('.' + $button.attr('class')).length) 
                $button.remove();
        });
    
        $dropZone.append($buttons);
    });​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a group of radio buttons on my page like this: ALL (x)
I have this group of radio buttons in which each of individual button has
I have a group of three radio buttons. Depending on which radio button is
I have a group of buttons that should act like toggle buttons, but also
I have 2 radio button with 2 group. The structure is like this Main
I have a function for which i am sharing a group of link buttons
I have a group of buttons on my web page which all have an
I have created a radio group which contains two buttons, I can select one
I have added a group of radio buttons dynamically to the DOM using jQuery.
I have a html form which contains group of radio buttons. When one of

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.