How to add an Array of classes to an element using jQuery? Ex. Add different color to different using [array]

With jQuery 3.3.0, the .addClass().removeClass(), and .toggleClass() methods accept an array of classes. We will see how to add different color to n number of div elements on jQuery 1.11.0

In this scenario, we will be applying different class to 7 box. The HTML result looks like this

html

The colors from array are assigned to the element using .each function. jQuery logic is below:

jquery

and CSS to design the boxes

css

The final result is shown below

result

See fiddle.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s