Home > Blockchain >  Add class if image has the class
Add class if image has the class

Time:06-28

I have some images which are way too big when I make the menu they're containing in smaller, that's 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 as well, 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" they will still get the class "smalllorem" added on.

-> I don't get why images without the class "lorem" get into the code? I mean I ask if the image has class .. Why does it include 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
}

var count = 0;
function resize(){
var menue = document.getElementById("container");
count  ;
if(count % 2)
{
menue.style.width = "50%";
menue.style.height = "50px";
}
else
{
menue.style.width = "100%";
menue.style.height = "100px";
}
}
#container{
border: 1px solid black;
width: 100%;
height: 100px;
transition: 330ms;
}
#home{
width: 15%;
height: auto;
min-width:10px:
}
menue
<div id="container">
<img src="https://cdn-icons-png.flaticon.com/512/25/25694.png" id="home">
</div>
<br>
<input type="button" value="resize menue" onclick="resize()">

  • Related