Home > front end >  add class if img has the class
add class if img has the class

Time:06-27

I have some images which are way to big when I make the menu which they're containing in smaller, thats why I made a second class where I changed the width and height.

I tried to add and remove the class with javascript like this:

    if ($('img').hasClass('lorem')) {
        $('img').removeClass('lorem')
        $('img').addClass('smalllorem')
    } else {
        $('img').addClass('lorem')
        $('img').removeClass('smalllorem')
    }

Now this works perfectly fine, but this will add the classes to my other images on the website aswell, how can I specify to only give the class "smalllorem" to the elements which have the class lorem ? Because the other images don't have the class "lorem" but they will still get the class "smalllorem" added on.

-> I don't get why images without the class "lorem" getting into the code ? I mean I ask if img has class .. Why it includes the other image elements ?

CodePudding user response:

I would look for a CSS solution before moving on to a JavaScript one. But answering the question asked...

I don't get why images without the class "lorem" getting into the code ? I mean I ask if img has class

Because $("img") selects all images, but $("img").hasClass("lorem") only looks at the first image to see if it has the class. Then in each branch of your if/else, you're applying changes to all images ($("img").addClass("lorem");). jQuery's API is asymmetric in this regard: methods that tell you something about the element only look at the first element in the jQuery collection, but methods that change something apply to all elements in the collection.

If I understand you correctly, you want to:

  • Remove lorem from images that have it, adding smalllorem instead

    and

  • Remove smalllorem from images that have it, adding lorem instead

Basically, you want to toggle both classes. There's a toggleClass method for that.

$("img.lorem, img.smalllorem").toggleClass("lorem smalllorem");

That selects all img elements that have either class, and toggles the classes on them.

Live Example:

setTimeout(() => {
    $("img.lorem, img.smalllorem").toggleClass("lorem smalllorem");
}, 800);
.lorem {
    border: 2px solid black;
}

.smalllorem {
    border: 2px solid yellow;
}
<div>lorem (black border) => smalllorem (yellow border):</div>
<img  src="https://via.placeholder.com/50.png/09f/fff">
<img  src="https://via.placeholder.com/50.png/09f/fff">
<img  src="https://via.placeholder.com/50.png/09f/fff">

<div>smalllorem (yellow border) => lorem (black border):</div>
<img  src="https://via.placeholder.com/50.png/09f/fff">
<img  src="https://via.placeholder.com/50.png/09f/fff">
<img  src="https://via.placeholder.com/50.png/09f/fff">

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

CodePudding user response:

Instead of adding a new class to the image you could just make it responsive :

.img {
  width: 100%;      //define width
  max-width: 250px; //restrict the size (can use min-width aswell)
  height: auto;     //auto adjust depending on the width
}
  • Related