So I have multiple div's with the same class and inside them images that can be from 1 to 4 depending on the div, I do not know in advance how many images because they come from the database. How can I give the images different classes depending on how many there are in the div. MY html
<section class="feed-section">
<article class="feed-article">
<div class="question-options">
<img src="http://placehold.it/600x400">
</div>
</article>
<article class="feed-article">
<div class="question-options">
<img src="http://placehold.it/600x400">
<img src="http://placehold.it/600x400">
<img src="http://placehold.it/600x400">
<img src="http://placehold.it/600x400">
</div>
</article>
<article class="feed-article">
<div class="question-options">
<img src="http://placehold.it/600x400">
<img src="http://placehold.it/600x400">
<img src="http://placehold.it/600x400">
</div>
</article>
</section>
my js
function imagesWidth() {
var imagesConatiner = $('.question-options').each(function (index) {
var images = $(this).children('img').length;
switch (images) {
case 1:
$('.question-options img').addClass('one')
break;
case 2:
$('.question-options img').addClass('two')
break;
case 3:
$('.question-options img').addClass('three')
break;
case 4:
$('.question-options img').addClass('four')
break;
default:
console.log(images)
}
})
};
imagesWidth()
The problem now is that it ads multiple classes for example to all the images it adds one for three
I want to do them with css like
img.one {
width:100%
}
img.two {
width:50%
}
And so on...