How do I remove a class from a parent element and apply it to a child using jQuery?

My auto-generated menu in wordpress has this kind of layout:

<li class="cmsms-icon-star-1 menu-item menu-item-type-post_type menu-item-object-page menu-item-2542" id="menu-item-2542"><a href="">ARTICOLI NUOVI</a></li>

<li class="cmsms-icon-wheelchair menu-item menu-item-type-post_type menu-item-object-page menu-item-2542" id="menu-item-2542"><a href="">ARTICOLI NUOVI</a></li>

Open in new window

I would like it to be come:
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2542" id="menu-item-2542"><a class="cmsms-icon-star-1 " href="">ARTICOLI NUOVI</a></li>

<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-2542" id="menu-item-2542"><a class="cmsms-icon-wheelchair " href="">ARTICOLI NUOVI</a></li>

Open in new window

Basically, I would like ANY class in the <li> parent that has initial part "cmsms-icon-" to be moved to child <a> tag.

thanks in advance
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
You can try this
<script type="text/javascript">
$(function() {
	$('[class^="cmsms-icon-"]').removeClass(function(i, cls) {
		var target = cls.match(/(^|\s)cmsms-icon-\S+/g) || [];
		$('a', this).addClass(target[0]);
		return target[0];

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
badwolfffAuthor Commented:
Hi Julian,

just a out of curiosity, since I am learning this stuff now, could you please explain a couple of bits of your code please?

function(i, cls)

cls.match(/(^|\s)cmsms-icon-\S+/g) || []

Julian HansenCommented:
The code searches for an element with a class that starts with cmsms-icon.

(A) For each one that it finds it calls removeClass to which it passes a function the return value of which is used by removeClass.

The function uses a regular expression to find an instance of any class starting with cmsms-icon. The first item in the returned array will be the entire matched string which will be the full matched class.

This class is added to the <a> elements of the matched class (in A above) and then returned to the removeClass so that it can be removed from the parent.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

badwolfffAuthor Commented:
Thanks for the explanation
I have a few more questions
How would read these in english?

A: (/(^|\s)
B: \S+/g)
C: || []

Julian HansenCommented:
A) Match from start of string (^) or a space - in hindsight I am not sure of my motivation for including those - it was a while back - and I suspect the solution would work without them.

B) \S+ match one or more non-space characters

C: || [] - || means OR [] is the empty array - the reason for this is we are using the return value from match to remove / add the found class - if it is not found it returns false i.e. does not return an array in which case target[0] will generate an error. We therefore are saying if the return value is not false then use the return value otherwise use the empty array so our target[0] is valid in the following lines. The OR statement will be evaluated on the basis of the first statement to be not false - so if an array is returned that is what will be used if false is returned the next expression is evaluated ([]) which is not false so it is then used in the assignment.
badwolfffAuthor Commented:
Thanks :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.