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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:28:34+00:00 2026-06-07T19:28:34+00:00

>> JSFIDDLE << var imgFilterBtn = $(nav > ul > li > ul >

  • 0

>> JSFIDDLE <<

var imgFilterBtn = $("nav > ul > li > ul > li > a");

$("img").fadeIn("slow");

imgFilterBtn.click(function() {
    var fadeInClass = $(this).attr("id");
    var wrongFilter = $(this).parent("li").parent("ul").children("li").children("a");

    wrongFilter.removeClass(); // un-style all links in one sub-list
    $(this).toggleClass("selected"); // style selected link


    var wrongFilterID = wrongFilter.attr("id");
    $("#imgWrap").removeClass(wrongFilterID); // remove all classes from imgWrap that are equal to an ID all LI-items in  the current list


    $("#imgWrap").toggleClass(fadeInClass); // toggle the class that is equal to the ID of the clicked a-element

    $("img").hide(); // hide all images

    var imgWrapClass = $("#imgWrap").attr("class");
    $("#imgWrap").children("img." + imgWrapClass).fadeIn("fast"); // fade in all elements that have the same class as imgWrap
});   

I have done my best to include comments that explain what the script is doing.

1. What works:

  • The images fade in on document load
  • The “selected” class is toggled (but not toggled BACK!)
  • The class on #imgWrap is toggled, but doesn’t get toggled back
  • Images are hidden and show when a list-item (actually its parent li) is clicked

2. What does not work

  • When a li-item is clicked, the other classes don’t get deleted
  • Things mentioned above

3. What should happen
When a user clicks a link, the ID of that linked is passed on to a class which is assigned to #imgWrap. But before this class is assigned, all other classes that are the same as the ID’s of other list items of THE SAME LIST (so not of another list) get deleted. So, when you click black and fashion, and then brown #imgWrap should have the classes fashion and brown, and black should have been deleted.

I am guessing I am missing an each function but I’m not sure.

  • 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-07T19:28:35+00:00Added an answer on June 7, 2026 at 7:28 pm

    The problem seems to be that wrongFilter contains all a elements of that particular list and wrongFilter.attr("id") will always select the ID of the first selected element.

    Regarding the toggling: If you select the element that is already selected, you first remove the selected class and then add it again. Similar to the class added to #imgWrap.

    Restrict the selection to the actual selected element and fix the class adding/removal:

    // ...
    // Only get the currently selected element
    var wrongFilter = $(this).closest('ul').find('a.selected');
    var wrongFilterID = wrongFilter.attr("id"); // get its ID
    
    // toggle `selected` for the previous selected element and the current one;
    // will remove the class if the previous selected element is the same as the
    // current one 
    wrongFilter.add(this).toggleClass('selected');
    
    // ...
    
    // if the class already exists, the same menu entry was clicked and we have 
    // to remove the class
    $("#imgWrap").toggleClass(fadeInClass, wrongFilterID !== fadeInClass);
    
    // ...
    

    But now it can be that wrongFilterID is undefined and the next call to removeClass would remove all classes form #imgWrap. So you have to add a test:

    if(wrongFilterID) {
        $("#imgWrap").removeClass(wrongFilterID); 
    }
    

    Another problem is that imgWrapClass can be a space delimited string of classes, e.g. "fashion black", which means that

    .children("img." + imgWrapClass)
    

    will result in

    .children("img.fashion black")
    

    which is not what you want.

    You have to create a proper selector from that string, e.g.:

    // "fashion black" becomes "fashion.black"
    var imgWrapClass = $.trim($("#imgWrap").attr("class")).replace(/\s+/g, '.');
    

    With all that fixed, it seems to work properly:

    DEMO

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

Sidebar

Related Questions

Here an example http://jsfiddle.net/6GdW6/ $(.thumbnail).live(click, function() { $(.fullimage).hide(); var i = $(<img />).attr(src, this.href).load(function()
jsfiddle is here: http://jsfiddle.net/M86nA/ code is this: $('div.wrap').click(function(){ var $minY = $('div.imgur-wrap').height(); if ($minY
I would like help with http://jsfiddle.net/VmXU9/34/ . $(.click).click(function() { var s = $(this).offset(); left
http://jsfiddle.net/DerekL/whY4B/ $(p).each(function(i) { var btn = $('<button>button</button>'), span = $('<span>span</span>'); $(this).after(btn).after(span); });​ So here
http://jsfiddle.net/raylu/C6Tkn/ function a() { document.write(this + '<br />'); } a(); a.apply('hello'); var b =
Example http://jsfiddle.net/Rg8rf/ $(#label).change(function() { var src = $(this).data('value'); alert(src); }); How do I grab
http://jsfiddle.net/3NRsd/ var foo = $(div).bind(click, function() { $(div).animate({height : 500px}, 2000); $(div).animate({height : 50px},
http://jsfiddle.net/borayeris/6kvyb/ <ul> <li>foo</li> <li>bar</li> </ul> <script> $('li').each(function(index) { var qq=$(this).text(); alert(index + ': '
I have this code (JSFiddle) var OBJ = function(){ var privateVar = 23; var
JSFiddle: http://jsfiddle.net/KH8Gf/27/ Code: $(document).ready(function() { $('#expand').click(function() { var qty= $('#qty').val(); for (var counter =

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.