How do I apply a specific class to a specific child using jQuery?

I have an autogenerated code in wordpress for my menu that looks like this (this is just an excerpt, as every sub-item in the menu is similar):

<li class="mfcustom-icon-mission-family menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-1138 current_page_item menu-item-2385"><a href="http://demo.missionfamily.org/content/chi-siamo/mission-family/"><span>Mission Family</span></a>	</li>

<li class="mfcustom-icon-v-pay menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-1138 current_page_item menu-item-2385"><a href="http://demo.missionfamily.org/content/chi-siamo/mission-family/"><span>Mission Family</span></a>	</li>

Open in new window


I need a jquery that can check if an <li> tag has a class that starts with "mfcustom-icon-" and take that class and apply it also to the <span> inside the <li> so that the above code should become:

<li class="mfcustom-icon-mission-family menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-1138 current_page_item menu-item-2385"><a href="http://demo.missionfamily.org/content/chi-siamo/mission-family/"><span class="mfcustom-icon-mission-family">Mission Family</span></a>	</li>

<li class="mfcustom-icon-v-pay menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-1138 current_page_item menu-item-2385"><a href="http://demo.missionfamily.org/content/chi-siamo/mission-family/"><span class="mfcustom-icon-v-pay>Mission Family</span></a>	</li>

Open in new window


Thanks in advance
badwolfffAsked:
Who is Participating?
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.

hieloCommented:
Try:
$('li[class^="mfcustom-icon-"]').each(function(){
  /* get the css classes on the element and split them.  The first item ins css has mfcustom-icon-XXX since the selector we are using is a 'starts with' selector */
  var css = $(this).attr('class').split(' ');
  // take the class in css[0] and add it to the span within the current LI
  $('span',this).addClass( css[0] );
});

Open in new window

0

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
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
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.