I need to add to specific elements of a list the class .disabled
.
These elements can be found in an array or something like that - don't know what is remended for this.
HTML:
<ul id=anything>
<li><a href="javascript:;" class="re-icon re-deleted"></a> Text</li>
<li><a href="javascript:;" class="re-icon re-bold"></a> Text</li>
<li><a href="javascript:;" class="re-icon re-italic"></a> Text</li>
<li><a href="javascript:;" class="re-icon re-table"></a> Text</li>
</ul>
This should bee this:
<ul id=anything>
<li><a href="javascript:;" class="re-icon re-deleted"></a> Text</li>
<li><a href="javascript:;" class="re-icon re-bold disabled"></a> Text</li>
<li><a href="javascript:;" class="re-icon re-italic"></a> Text</li>
<li><a href="javascript:;" class="re-icon re-table disabled"></a> Text</li>
</ul>
JS:
var arr = ['re-bold', 're-table'];
$('#anything a').each(function() {
if ( $(this).hasClass('anything of the array') ) { // Check if this element has a class, which is given in the array
$(this).addClass('disabled');
});
I need to add to specific elements of a list the class .disabled
.
These elements can be found in an array or something like that - don't know what is remended for this.
HTML:
<ul id=anything>
<li><a href="javascript:;" class="re-icon re-deleted"></a> Text</li>
<li><a href="javascript:;" class="re-icon re-bold"></a> Text</li>
<li><a href="javascript:;" class="re-icon re-italic"></a> Text</li>
<li><a href="javascript:;" class="re-icon re-table"></a> Text</li>
</ul>
This should bee this:
<ul id=anything>
<li><a href="javascript:;" class="re-icon re-deleted"></a> Text</li>
<li><a href="javascript:;" class="re-icon re-bold disabled"></a> Text</li>
<li><a href="javascript:;" class="re-icon re-italic"></a> Text</li>
<li><a href="javascript:;" class="re-icon re-table disabled"></a> Text</li>
</ul>
JS:
var arr = ['re-bold', 're-table'];
$('#anything a').each(function() {
if ( $(this).hasClass('anything of the array') ) { // Check if this element has a class, which is given in the array
$(this).addClass('disabled');
});
Share
Improve this question
edited Oct 22, 2019 at 7:02
Jeroen
1,1661 gold badge12 silver badges24 bronze badges
asked Sep 6, 2014 at 22:00
user3142695user3142695
17.4k55 gold badges194 silver badges375 bronze badges
1
- possible duplicate of jQuery hasClass() - check for more than one class – Stephan Weinhold Commented May 8, 2015 at 9:10
3 Answers
Reset to default 8You could add a .
to the array's elements, either manually or by using the Array.prototype.map
method. and join the array's elements. Then filter
method will filter the matching elements according to the created selector:
var arr = ['.re-bold', '.re-table'];
$('#anything a').filter(arr.join()).addClass('disabled');
One way:
var arr = ['re-bold', 're-table'];
$('#anything a').each(function () {
for (var i = 0; i < arr.length; i++) {
if ($(this).hasClass(arr[i])) $(this).addClass('disabled');
}
});
jsFiddle example
Produces:
<ul id="anything">
<li><a href="javascript:;" class="re-icon re-deleted"></a> Text</li>
<li><a href="javascript:;" class="re-icon re-bold disabled"></a> Text</li>
<li><a href="javascript:;" class="re-icon re-italic"></a> Text</li>
<li><a href="javascript:;" class="re-icon re-table disabled"></a> Text</li>
</ul>
Just iterate through the array, to check for each class:
var arr = ['re-bold', 're-table'];
$('#anything a').each(function() {
var i = 0,
arrayLength = arr.length;
for(; i < arrayLength; i++){
if ( $(this).hasClass(arr[i]) ) {
$(this).addClass('disabled');
}
}
});
Demo: http://jsfiddle/e6mds7vz/1/