Sathya
Sathya

Reputation: 1734

How to match the class name from an array in jquery?

I have stored all classes an array. And also i added one attribute an element, that is also included an array. I want to add attribute value as class on triggered element. How to implement this logic?

var $coverPosition = ['left', 'right', 'top', 'bottom', 'centerxy']
$('.posRow').on('click', function(){
  $('.coverBg').removeClass($coverPosition).addClass($(this).data('class'));
});

My html structure is,

<div class="coverBg left">
</div>
<a href="#" class="posRow" data-class="top">
</a>

Sometimes coverBg element class may be 'left right'

Upvotes: 2

Views: 892

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167162

Use this way, if you wanna do it for all the classes in the array:

var $coverPosition = ['left', 'right', 'top', 'bottom', 'centerxy']
$('.posRow').on('click', function(){
  $('.coverBg').removeClass($coverPosition.join(" ")).addClass($(this).data('class'));
});

Upvotes: 7

Related Questions