Jason
Jason

Reputation: 4957

How to Extract Part of a String In JavaScript

I am using a chunk of jQuery code that returns a selected option from a group of image icons. It is integrated into the WordPress WYSIWYG Editor.

When I click on the icon this string is sent to the editor:

image="'+ $('.icon-option i.selected').attr('class') +'"

This code returns: fa fa-heart selected

From this string I want to remove the string selected so that it just reads fa fa-heart.

I tried: image="'+ $('.icon-option i.selected').attr('class').split(' ')[0] +'" but it cut the string off at fa

Is this an easy fix that can be added on to my original jQuery String manipulation call?

Upvotes: 0

Views: 67

Answers (2)

scrappedcola
scrappedcola

Reputation: 10572

No need for string manip, use the removeClass method:

$('.icon-option i.selected').removeClass("selected");

If you then also need the string of the class names you can expand to:

$('.icon-option i.selected').removeClass("selected").attr("class");

Upvotes: 2

Richard Kho
Richard Kho

Reputation: 5286

You can accomplish this by doing:

image="'+ $('.icon-option i.selected').attr('class').split(' selected')[0]

Upvotes: 2

Related Questions