Solved

jQuery select class contain part of name

Posted on 2010-08-29
6
832 Views
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">
0
Comment
Question by:palserv
[X]
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
6 Comments
 
LVL 7

Expert Comment

by:mcuk_storm
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

li.Bleu1.Selected

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
0
 
LVL 7

Expert Comment

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

Accepted Solution

by:
leakim971 earned 250 total points
ID: 33554223
You may use : http://api.jquery.com/attribute-contains-selector/


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

Open in new window

0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 82

Expert Comment

by:leakim971
ID: 33554225
Test page :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script language="javascript">
	$(document).ready(function() {
		$("#but").click(function() {
			$("li[class*='Selected']").each(function() {
				prevClass = $(this).attr("className");
				nextClass = prevClass.replace("Selected","");
				$(this).removeClass(prevClass).addClass(nextClass);
			});
		});
	});
</script>
</head>
<body>
<input type="button" id="but" value="click me" />
<ul>
    <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>
</ul>
</body>
</html>

Open in new window

0
 
LVL 6

Assisted Solution

by:hehdaddy
hehdaddy earned 250 total points
ID: 33555826
Simple:

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

Give this a go!
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33602698
Thanks for the points!
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…

691 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