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

  • Home
  • SEARCH
  • 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 7577273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:08:34+00:00 2026-05-30T17:08:34+00:00

While writing an add to favorites-function using AJAX and certain other jQuery functions like

  • 0

While writing an “add to favorites”-function using AJAX and certain other jQuery functions like “bind” and “clone” a problem appeared. I will first explain the functionality:

I have two content areas:

  1. Product list – containing all products (Product title, picture and it’s “add to favorites”-function)
  2. Favorites – containing all products which have been marked as favorites (element has the same html-structure as product)

When adding a product to favorites I programmatically change the “add to favorites”-button to a “remove from favorites”-button. Additionally, I clone the entire product (including the “remove from favorites”-button) to the “Favorites”-area.

Now, if I switch to the “Favorites”-area and try to remove the favored product, it doesnt’t work. If I then refresh the page, the removing works perfectly..

I my opinion, it’s got to do with refreshing the DOM structure or re-binding the function or something like this..
I’m very happy for any kind of help or explanations on this problem.

Here’s my code (script is placed at the end of the page):

// ACTION: Favorites
$JQuery('.katoverview_media').find('.prod_action_favorit').bind("click", function(){
    //Split data and set variables
    var type    = $JQuery(this).attr("rel").split('%')['0'];
    var objid   = $JQuery(this).attr("rel").split('%')['1'];
    var favs    = parseInt($JQuery('#count_favoriten').html());
    if($JQuery(this).hasClass("active")){
        $JQuery(this).removeClass("active");
        var action      = 'delete';
        var favs_new    = favs-1;   
        $JQuery.each($JQuery('.katoverview_media_fav').find('.prod_action_favorit'), function() {
            var checkObjid  = $JQuery(this).attr("rel").split('%')['1']; 
            if(checkObjid == objid){
                if($JQuery(this).hasClass("active")){
                    $JQuery(this).parent().parent().parent().remove();
                }
            }
        });
    } else {
        $JQuery(this).addClass("active");
        var action      = 'add';
        var favs_new    = favs+1;
        $JQuery(this).parent().parent().parent().clone().appendTo('.katoverview_media_fav');
    }
    var pars = 'type='+type+'&objid='+objid+'&action='+action;
    $JQuery.ajax({url: "ajax/fav.php?"+pars});
    $JQuery('#count_favoriten').html(favs_new).css({color: 'red'}).delay('500').animate({color: '#333'}, 1000);
})

$JQuery('.katoverview_media_fav').find('.prod_action_favorit').bind("click", function(){
    //Split data and set variables
    var type    = $JQuery(this).attr("rel").split('%')['0'];
    var objid   = $JQuery(this).attr("rel").split('%')['1'];
    var favs    = parseInt($JQuery('#count_favoriten').html());
    if($JQuery(this).hasClass("active")){
        $JQuery(this).parent().parent().parent().remove();
        var action      = 'delete';
        var favs_new    = favs-1;   
    }
    var pars = 'type='+type+'&objid='+objid+'&action='+action;
    $JQuery.ajax({url: "ajax/fav.php?"+pars});
    $JQuery('#count_favoriten').html(favs_new).css({color: 'red'}).delay('500').animate({color: '#333'}, 1000);
    $JQuery.each($JQuery('.katoverview_media').find('.prod_action_favorit'), function() {
        var checkObjid  = $JQuery(this).attr("rel").split('%')['1']; 
        if(checkObjid == objid){
            if($JQuery(this).hasClass("active")){
                $JQuery(this).removeClass("active");
            }
        }
    });
})
  • 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-30T17:08:35+00:00Added an answer on May 30, 2026 at 5:08 pm

    .clone( [withDataAndEvents] [, deepWithDataAndEvents] )

    Normally, any event handlers bound to the original element are not copied to the clone. The optional withDataAndEvents parameter allows us to change this behavior, and to instead make copies of all of the event handlers as well, bound to the new copy of the element. As of jQuery 1.4, all element data (attached by the .data() method) is also copied to the new copy.

    As of jQuery 1.5, withDataAndEvents can be optionally enhanced with deepWithDataAndEvents to copy the events and data for all children of the cloned element.

    For instance

    $JQuery(this)
      .parent()
      .parent()
      .parent()
      .clone(true, true) /* with withDataAndEvents and deepWithDataAndEvents  */
      .appendTo('.katoverview_media_fav');
    

    might be what you need.

    Otherwise, use .live() (or preferably .on() for jQuery 1.7+) instead of .bind().

    $('.katoverview_media_fav .prod_action_favorit').live("click", function(){
       ...
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing my code like this: function updates_cb() { in a while loop
While writing a file using ofstream, how do I know when the file's size
This popped-up while writing a script for vim. Using the well-documented ':normal' command: :normal
There are certain guide lines to be followed while writing Html for you page.
I am writing a function that well keep the user in lightbox images while
I'm writing a rudimentary screen scraper tool as an FF add-on (using DOM and
I am writing one web application using PHP/CodeIginter. Now, I want to add a
Please tell me when I add in the xml while writing <![CDATA[ content ]]>
I've come across a problem while writing a custom Adapter for the items on
I'm facing one issue while writing Office Outlook 2007 Add-In, I'm accessing the email

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.