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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:01:17+00:00 2026-05-20T10:01:17+00:00

I am trying to implement a JqueryUI button called favorites (id=#favorites) for thumbnails in

  • 0

I am trying to implement a JqueryUI button called favorites (id=”#favorites”) for thumbnails in an image gallery. The button toggles the display of the favorites icon (span class=”fav”). I am using Isotope for the thumbnail layout which allows sorting and filtering. What I am trying to do is add a class of “favorites” to the Isotope DIV (class=”item”) to allow filtering of the images when the “fav” icon is clicked for an individual thumbnail. I also want my favorites icon to stick to each thumb that is marked as a favorite and not toggle after chosen when the JqueryUi button is clicked.
So far I am able to make the icons stay visible and add the class to the Isotope DIV except that it adds the class to all DIV’s as well as all the favorite icons stay visible. I need to have it only alter the specific DIV and favorite icon of that particular thumbnail.
I am still learning jquery and my code is rudimentary and may be totally wrong for this purpose. Any help or direction would be appreciated!

Here is a link: Gallery
Clicking the “Fav” button toggles the icons

HTML:

<div class="item">
    <a href="image.jpg" title="Image">
        <img src="image.jpg" width="80" height="80" alt="Image" />
    </a>
    <span class="fav ui-icon ui-icon-heart"></span>
    <div class="title">Image 1</div>
</div>

Button Code:

// JQuery Ui button setup
$( "#favorites" ).button({
    label: "Fav",
    icons: { primary: "ui-icon-heart" }
});

// initially hides the icon
$('span.fav').hide();

//click function to toggle icon under thumbs
$("#favorites" ).click(function() {
    $('span.fav').slideToggle('slow'); 
    return false;
});

// my attempt at adding a class 'sticky' to icon to make it stay visible as well as add class '.favorites' to '.item' div to set it up for filtering
$("span.fav" ).live('click', function() {
    $('.fav').addClass("sticky");

    $('.fav').each(function ()
    {
        $(this).removeClass("fav");
    });  
    $('.item').each(function ()
    {
        $(this).addClass("favorites");
    });  
    return false;
});

I used .live because Isotope is making dynamic changes to the code but this may be not the right thing to do.

  • 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-20T10:01:17+00:00Added an answer on May 20, 2026 at 10:01 am

    To me it looks like the whole issue may be just an incorrect understanding of the jQuery functions you’re using.

    $("span.fav" ).live('click', function() {
        // This adds the 'sticky' class to EVERY html element on the page that matches '.fav'
        // $('.fav').addClass("sticky");
        // Try this:
        $(this).addClass("sticky");
    
        // This is a similar problem here.  You're getting everything that matches '.fav'
        // and you REALLY want just the current item you clicked on.
        /*
        $('.fav').each(function ()
        {
            $(this).removeClass("fav");
        });
        */
        // Try this instead:
        $(this).removeClass("fav");
    
        // Again, you don't want every '.item', but just the one related to the favorite
        // that you just clicked
        /*
        $('.item').each(function ()
        {
            $(this).addClass("favorites");
        });
        */
        // Do this instead, using $(this) as the DOM element where you're starting from:
        // First, get to the icon's parent, which is the specific 'div.item' you want
        // and then just add the class to that one
        $(this).parent().addClass('favorites');
    
        // You really only need return false on stuff like <a> tags to prevent
        // them from doing what they would normally want to, and GO somewhere
        // return false;
    });
    

    The problem lies simply that you need to be aware of the this variable, which in will reference the current object you’re interacting with. In the case of the ‘click()’ method, this refers to the element that was clicked.

    A shorter version of the code would be this (which also allows removing of favorites):

    $("span.fav").live('click', function() {
      $(this).toggleClass('sticky')
        .toggleClass('fav')
        .parent()
        .toggleClass('favorites');
    });
    

    This takes advantage of jQuery’s chaining, allowing you to call method after method on something. Notice only one ‘;’ in the function 🙂

    (Thanks for formatting and commenting your stuff. So much easier to understand and answer!)

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

Sidebar

Related Questions

I'm trying to use jQueryUI Autocomplete to implement a site quick searching feature for
Ok well I'm trying implement something similar to the 'undo' function in many image
Im trying to implement an asp.net textbox using the jQuery UI Autocomplete widget http://jqueryui.com/demos/autocomplete/#remote
I am trying to implement fancybox and jqueryui's accordion into my site, I can
SetFocus I'm trying implement the above Se Focus code in a Class Library that
I am trying implement a photo gallery similar to facebook in Android. I was
I'm trying to implement a jqGrid with MVC. The Model is: public class InvoiceHeader
I have a site where I am trying to implement a jQuery UI based
All I am currently trying implement something along the lines of dim l_stuff as
trying to implement a dialog-box style behaviour using a separate div section with all

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.