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.

jQuery, select all elements with the same class

Featured Replies

What I want is to click any <li> in a <ul>, and then affect ALL elements with the same class on the entire page.

$("anyclass").click(function(){
	$("anyclass").hide();
});

I don't want to manually enter the element classes, as the client can add additional items to the list and their class is a random ID.

 

I feel like it should be a very small piece of jquery but I can't think for life of me.

  • Author

Just a little more info.

$(".icon.id-55").click(function(){
	$(".id-55").toggleClass("active");
});

When you click the .icon with .id-55, it also toggles the .active for a panel below with the .id-55 class.

 

I essentially want to replace the id-55 with id-n

 

Any thoughts?

Something like this https://jsfiddle.net/trpfq3m5/? It's a little fragile, because it depends on the numbered class being after the first class (classList[1]). I still think it's better than doing it dynamically with a count, because they tend to always be incorrect, especially if the class names have been generated by a CMS.

 

edit: updated jsfiddle example

Edited by Jack

  • Author

Something like this https://jsfiddle.net/trpfq3m5/? It's a little fragile, because it depends on the numbered class being after the first class (classList[1]). I still think it's better than doing it dynamically with a count, because they tend to always be incorrect, especially if the class names have been generated by a CMS.

 

edit: updated jsfiddle example

Thank you Jack this seems to do what I want. I'll try applying it tomorrow.

 

Just out of curiosity, what does the + el do?

('.panel-' + el)

 

Thank you Jack this seems to do what I want. I'll try applying it tomorrow.

 

Just out of curiosity, what does the + el do?

('.panel-' + el)

 

el is just a variable (short for element) that contains the number part of the class. The plus is concatenating both the class name (panel-) and the number together.

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.