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

185
Visualizações
jQuery: if/else statement: it cant read the else statement it reads only first if

I have basic if-else statement. it cant read after if parts which are else if and else. İt only reads first if. this is my code. What might be the problem here.

if (screen.width < 992) {
alert('Less than 992');
 $("#res_img").toggleClass('col-lg-6 col-lg-5');
 $("#res_img_two").toggleClass('col-lg-6 col-lg-7');

 } else if (screen.width < 425) {

 alert('Less than 425');
 $("#res_img").toggleClass('col-5 col-12');
 $("#res_img_two").toggleClass('col-7 col-12');

 }
 else {
   console.log('success');
 }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

first of all, your code will run only once every page reload, be aware of that in testing. Second of all, let's rewrite what you wrote in code in plainer "pseudo" instructions:

if width of the screen is less than 2 pixels {
    say 'Less than 2';
 } else if width of the screen is less than 1 pixel {
    say 'Less than 1';
 }
 else {
    say 'success';
 }

Maybe this way it is more clear, that every screen that is less than 1 will be always less than 2 pixels, so, really, there is no real way to separate those two cases, when you ask this way. We can try to rearrange this question like this:

if width of the screen is less than 1 pixels {
    say 'Less than 1';
 } else if width of the screen is less than 2 pixel {
    say 'Less than 2';
 }
 else {
    say 'success';
 }

Then every screen that is less than 1 will be always less than 2 pixels, and if it is more than 1 but less than 2, then only the second statement will be true. But I think our intentions can be written more clearly as:

if width of the screen is less than 2 pixels and width of the screen is more than 1 {
    say 'Less than 2 but more than 1';
 } else if width of the screen is less than 1 pixel {
    say 'Less than 1';
 }
 else {
    say 'success';
 }
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