Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

97
Visualizações
Add class if image has the class

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?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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 class="lorem" src="https://via.placeholder.com/50.png/09f/fff">
<img class="lorem" src="https://via.placeholder.com/50.png/09f/fff">
<img class="lorem" src="https://via.placeholder.com/50.png/09f/fff">

<div>smalllorem (yellow border) => lorem (black border):</div>
<img class="smalllorem" src="https://via.placeholder.com/50.png/09f/fff">
<img class="smalllorem" src="https://via.placeholder.com/50.png/09f/fff">
<img class="smalllorem" 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>

about 4 years ago · Juan Pablo Isaza Relatório

0

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()">

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda