Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Help with adding and removing classes

Featured Replies

Hi,

 

So, I have a filter that is just 4 words. When a word is clicked, a little bit of js adds the class 'selected' to it. When you click on another word it removes the class 'selected' and adds it to the new word clicked.

 

Here's the js for that:

 

    function selectNav() {
	  $(this)
	    .parents('ul:first')
		  .find('li')
		    .removeClass('selected')
		  .end()
	    .end()
	    .addClass('selected');
    }
    $('#portfolio-wrapper #portfolio-filter').find('li').click(selectNav);

 

Now what I want to do is apply a class to a particular li element further down the page, only if the word has the class 'selected'.

I've started like so:

 

if (jQuery("ul#portfolio-filter li").is(".all.clearfix.selected")) {
 jQuery('#portfolio-list').find('.all').addClass("show");
}
if (jQuery("ul#portfolio-filter li").is(".websites.clearfix.selected")) {
  jQuery('#portfolio-list').find('.all').removeClass("show");
  jQuery('#portfolio-list').find('.websites').addClass("show");
}

 

For some reason, the top if statement works but the bottom one doesn't - it's not adding and removing the 'show' class.

 

Can anyone tell me why this might be?

I'm not great at js but I really thought this wouldn't be too difficult!

 

Any help very much appreciated in advance,

-Jack

I can't entirely picture what you're trying to do, but perhaps hasClass() may work?

 

Sorry if not, I've got a cold which may explain my inability to picture what you're describing!

  • Author

I can't entirely picture what you're trying to do, but perhaps hasClass() may work?

 

Thanks. I tried that but it didn't work.

I read on that page that it's probably better to use 'is' when looking for long classes such as 'all.clearfix.selected'.

Have you got a link to what you're working on, or even an annotated screenshot? I have read through your post again but cannot understand what it is you're trying to achieve.

 

Things like the "word has the class selected" - say what?

Edited by andyl

  • Author

I can't divulge any more than I have unfortunately.

 

I've read through my OP again - try replacing 'word' with 'image' and see if that helps.

Are you trying to achieve something like this, where clicking on All would highlight both 'All' instances?

 

01iui.png

  • Author

Yes. Clicking 'All' would apply a class to all the boxes.

Then clicking 'Websites' would remove a class from all the boxes and apply a new one to all the 'website' boxes.

Etc.

Something like this?

 

<ul id="top-navigation">
   <li><a href="#all">All</a></li>
   <li><a href="#websites">Websites</a></li>
   <li><a href="#code">Code</a></li>
</ul>

<ul>
   <li class="portfolio-item"><img /></li>
   <li class="portfolio-item"><img /></li>
   <li class="portfolio-item"><img /></li>
   <li class="portfolio-item"><img /></li>
</ul>

<ul id="bottom-navigation">
   <li><a href="#all">All</a></li>
   <li><a href="#websites">Websites</a></li>
   <li><a href="#code">Code</a></li>
</ul>

<script type="text/javascript">
   $(function(){
       $("#top-navigation a, #bottom-navigation a").click(function(e){
           e.preventDefault();
           var type = $(this).attr("href").replace("#", "");
           $(".portfolio-item").attr('class', 'portfolio-item ' + type);
           $("#top-navigation a, #bottom-navigation a").removeClass("selected");
           $("#top-navigation a[href='#" + type + "'], #bottom-navigation a[href='#" + type +"']").addClass("selected");
       });
   });
</script>

 

Edit: Check the behaviour in Chrome or Safari inspector (class names specifically)

Edited by andyl

  • Author

Ok, I think we're getting somewhere.

Is it possible to change this line to add a class to the end of the current classes on the portfolio-items as opposed to overwriting them?

 

jQuery(".portfolio-item").attr('class', 'portfolio-item ' + type);

$(".portfolio-item").addClass(type);

 

But the class attribute will get very long and end up something like:

 

class="portfolio-item all websites all websites websites all all all"

  • Author

I know.

 

Actually there's a bigger problem here.

Here's what i'd like: you click a button at the top that says 'websites'. That button then gets a class called 'selected' added to it.

Then what I want it to do, is to find any of the li.portfolio-list items somewhere on the page that have the class 'websites' and to add a 'show' class to them.

 

If you were to click another button at the top (say the 'branding' button), it would remove the 'show' class from all the li.portfolio-list items and add it only to the 'branding' items. Currently, your code adds or remove 'websites' to all the li.portfolio-items.

 

Do you see what I mean?

Ah, I see.

 

This be your answer:

 

<ul id="top-navigation">
   <li><a href="#all">All</a></li>
   <li><a href="#websites">Websites</a></li>
   <li><a href="#branding">Branding</a></li>
</ul>

<ul>
   <li class="portfolio-item websites"><img /></li>
   <li class="portfolio-item branding"><img /></li>
   <li class="portfolio-item websites"><img /></li>
   <li class="portfolio-item branding"><img /></li>
</ul>

<ul id="bottom-navigation">
   <li><a href="#all">All</a></li>
   <li><a href="#websites">Websites</a></li>
   <li><a href="#branding">Branding</a></li>
</ul>

<script type="text/javascript">
$(function(){
    $("#top-navigation a, #bottom-navigation a").click(function(e){
        e.preventDefault();
        var type = $(this).attr("href").replace("#", "");
        $(".portfolio-item").removeClass("show");
        if (type != "all"){
        	$(".portfolio-item." + type).addClass("show");
        } else {
	        $(".portfolio-item").addClass("show");
        }
        $("#top-navigation a, #bottom-navigation a").removeClass("selected");
        $("#top-navigation a[href='#" + type + "'], #bottom-navigation a[href='#" + type +"']").addClass("selected");
    });
});
</script>

 

 

Edited to include 'all' functionality.

Edited by andyl

  • Author

That's spot on thanks!

 

It's for a filterable portfolio - i'm targeting the 'show' items with CSS and doing some nice fading animations using opacities and transitions.

There's an existing (and amazing) jQuery plugin for this. I've been searching for it for about 20 minutes but for the life of me can't remember the name. Keep a tab on this topic and I'll post it here if it comes to me. I remember being impressed with it when I saw it.

 

PS. No problem :good:

Edited by andyl

  • Author

If you're referring to the Isotope plugin then yes, i'm aware of it but it's a little to advanced for what I need in this particular situation.

Ah, well at least I can continue with my day now - that was driving me nuts! Hope you get it sorted buddy.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.