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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:48:32+00:00 2026-06-08T03:48:32+00:00

I have a site here where the user can click on some divs. Then

  • 0

I have a site here where the user can click on some divs. Then I add an “X” to it using this code:

function addEx( elem, insertClass ) {
        var element = $( elem );
        var newInsertClass = insertClass.replace('.', '');
        var insert = "<span class='" + newInsertClass + "'> X </span>";

        element.click(function(){
            var $this = $(this);

            if( $this.text().indexOf( "X" ) >= 0 ) {
                var id = $this.attr( "id" );
                $( "#" + id + " " + insertClass ).remove();
            } else {
                $this.append( insert );
            }   
        })
    }

As you can see, if the “X” is already there, I remove it. If it’s not there, I add it. This works fine.

The user can also click on a “get results” button. The divs the user has clicked are then compared with the right answers. According to this, I add classes to the divs that were clicked correctly, too much or that were forgotten to click. This also works, but the content (the “X”) gets lost. So I can see the CSS-stlying I supplied for these classes, but the “X” inside the div is missing after replacing the original with the clone. For adding the classes I use this code:

var test = $(".test");
var clone = test.clone(true,true);


function correction() {
    clone.children().filter(function() {
        return $.inArray('#' + this.id, forgotten) != -1;
    }).addClass("forgotten").end().filter(function() {
         return $.inArray('#' + this.id, toomuch) != -1;
    }).addClass("toomuch").end().filter(function() {
         return $.inArray('#' + this.id, correct) != -1;
    }).addClass("right");

   test.replaceWith(clone);                   
 }

I clone the element here, then add the appropriate classes on the clone and replace the original with the clone.

So my question is: How can I prevent this loss of content? I guess it’s because the “X” are added dynamically? Or I also have more then one element with .test – could this be the cause?

  • 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-08T03:48:35+00:00Added an answer on June 8, 2026 at 3:48 am

    The John Resig has answered a question about cloning objects using jQuery. It might help you

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

Sidebar

Related Questions

I have a site showing lots of products here When you click on the
I have a site thats served in 2 flavours, English and French. Here's some
this is my first question.. so, here we go. i have a site, 100%
I have a some elements, roughly like this: <div> <a> <div> When a user
Okay so here's my issue. The user can go onto my site and retrieve
So I have some function that expand and collaps based on hover and click.
In a site I am managing, I have this styled link that on click
Yo! I have the following function implemented on my site, which allows the user
We have a Joomla 1.5 site here : http://printversal.com It got hacked last night.
I have a fixed footer on my site here: http://starprovisions.com/dev/bacchanalia.html On my 1360x768 screen

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.