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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:13:59+00:00 2026-05-24T12:13:59+00:00

Recently I posed a question , asking why some of my javascript code was

  • 0

Recently I posed a question, asking why some of my javascript code was misbehaving. However, the accepted answer didn’t fully solve my problem, so here I am once again.

Problem Description

  • I have a <div> which has a collection of radio buttons in it.
  • I use jquery ui to style that collection with a buttonset(). It looks resonably pretty.
  • Then I empty the <div> with jquery by doing something to the effect of $("#mydiv").html("")
  • Then I once again restore exact contents that were removed.
  • Finally the buttonset is no longer working properly, because its events got unhinged in the process.

So my question is how to protect such bound events from being garbage collected, when I temporarily tinker with the DOM?

NB! I can’t do display:none to hide the <div> instead, because the whole business with deleting html content and restoring it later is handled by an unnamed jquery plugin. Nor can I call buttonset() again, because a) the graphic style gets messed up, and b) there are other controls in my real problem that don’t have this handy functionality. So what I really need is some way to protect all those handlers while the elements which behavior they are supposed to govern are temporarily missing from the DOM.

Sample Code

HTML

<div id="container">
    <div id="buttonset">
        <input type="radio" id="radio1" name="option" />
        <label for="radio1">X</label>
        <input type="radio" id="radio2" name="option" />
        <label for="radio2">Y</label>
        <input type="radio" id="radio3" name="option" />
        <label for="radio3">Z</label>
    </div>
</div>

<div id="control">
    <input id="toggle" type="checkbox"/>
    <label for="toggle">Toggle</label>
</div>

Javascript

$(function(){
    $("#buttonset").buttonset();  
    $("#toggle").click(
        function(){
            if($(this).is(":checked"))
            {
                backup = $("#container").html();
                $("#container").html("");
            } else $("#container").html(backup);
        }
    )
});

Playable Version

See this jsFiddle

Solution

I used the idea in the accepted answer to save html contents before applying buttonset(), then reapply buttonset() each time as needed on that saved data.

  • 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-05-24T12:14:01+00:00Added an answer on May 24, 2026 at 12:14 pm

    Update:

    Here’s an updated fiddle that’s pretty close to what your OP is. The basic idea is it destroys the buttonset to get the original html back

    $(function() {    
    
        //Turn the radio buttons into a jquery ui buttonset
        $("#buttonset").buttonset();
    
        //Use the toggle button to hide/show the #container div.
        //NB! It's not possible to do css display:none instead,
        //due to some other limitations, to wit, I'm using a
        //library that I can not modify.
        $("#toggle").button();
    
        var backup;      // added this to prevent it from leaking onto global scope.
    
        $("#toggle").click(function() {
            if ($(this).is(":checked")) {
    
                // restore the html back
                $("#buttonset").buttonset("destroy");
    
                backup = $("#container").html();
                $("#container").html("");
            } 
            else { 
                $("#container").html(backup);
                $("#buttonset").buttonset();
            }
    
        })
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me pose a bit of background information before asking my question: I recently
I recently posted a question asking for what actions would constitute the Zen of
There was a post on here recently which posed the following question: You have
Interesting problem posed by a friend recently: Imagine you've got a List< NodeType >
I recently posted this question which worked locally with the answer I selected as
Apologies if this question is already posted. I didn't find the answer i was
Please see here for a question I recently asked. The only answer you should
I recently posted a question asking how I can return a File from a
Hello I recently posted this question but very badly explained the problem, but i'll
I recently posted a question about Azure... is it really an OS? I understand

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.