3

Suppose I have an element look likes

<div class="amy mary peter john tom">abc</div>

I will use the jQuery to get the class name

var classname = $(div).attr('class');

Since the result is "amy mary peter john tom", how can I convert this string to array?

Somethings likes var classname = ["amy", "mary", "peter", "john", "tom"];

Thanks

2 Answers 2

8

Use the string split function:

var classname = $('div').attr('class').split(/\s+/);
Sign up to request clarification or add additional context in comments.

Comments

1

for Example : you have : and you want to get the 'class2'

just add this. hope will help this. $('div').attr('class').split(' ')[1];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.