jQuery select class contain part of name

Posted on 2010-08-29
Last Modified: 2012-05-10
in my html i have the following
<li class="Bleu1" id="2">
<li class="Bleu1Selected" id="3">
<li class="yallow1Selected" id="4">
<li class="yallow1" id="5">

i want jQuay that will select all (li) with class name contain (Selected)
and then reset the class to be without (Selected)
Like this

<li class="Bleu1" id="2">
<li class="Bleu1" id="3">
<li class="yallow1" id="4">
<li class="yallow1" id="5">
Question by:palserv
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Expert Comment

ID: 33554094
Do you have control of the markup? If so i would suggest using multiple classes instead of a compound one i.e.
<li class="Bleu1 Selected">

You can then identify this through css/jQuery like


then if you want to remove selected you can do $('li.Bleu1.Selected').removeClass('Selected');

This makes selecting all Selected ones alot easier and likewise all Bleu1 ones easier

Expert Comment

ID: 33554102
With the above comment, an example to reset all selected would simply be $('.Selected').removeClass('Selected');
LVL 82

Accepted Solution

leakim971 earned 250 total points
ID: 33554223
You may use :

$("li[class*='Selected']").each(function() {
				prevClass = $(this).attr("className");
				nextClass = prevClass.replace("Selected","");

Open in new window

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

LVL 82

Expert Comment

ID: 33554225
Test page :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src=""></script>
<script language="javascript">
	$(document).ready(function() {
		$("#but").click(function() {
			$("li[class*='Selected']").each(function() {
				prevClass = $(this).attr("className");
				nextClass = prevClass.replace("Selected","");
<input type="button" id="but" value="click me" />
    <li class="Bleu1" id="2">Hello</li>
    <li class="Bleu1Selected" id="3">Hello</li>
    <li class="yallow1Selected" id="4">Hello</li>
    <li class="yallow1" id="5">Hello</li>

Open in new window


Assisted Solution

hehdaddy earned 250 total points
ID: 33555826

    var class = $(this).attr("class");
    class.replace("Selected", "");
    $(this).attr("class", class);

Give this a go!
LVL 82

Expert Comment

ID: 33602698
Thanks for the points!

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Rotate images for Web Page 11 28
Html split(text) Part2 6 25
what should i learn? javascript or typescript? 3 41
Slush on text 2 12
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question