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

252
Visualizações
If section has class active then add it's id to body as a class?

I would be grateful if you help with this thing, I can't resolve. I am trying to create a function: "if any section has class "active" => take this section's id then add it to body as a class element otherwise remove"

This the code below, but it's not working properly or when I scroll to next section new class is not being added and previous one removed.

<body class="">
    <section id="home" class="active">
    </section>

    <section id="about" class="">
    </section>
</body>

if ($('section').hasClass('active')) {
    var dataSection = $('section').attr('id');
    $body.addClass(dataSection.replace('#', '') + '-active');   
} else {
    $body.removeClass('home-active');
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Try using this here.

if( $(something).hasClass("active") ){
    var grabId = $(this).attr("id");
    $("body").attr("id", grabId);
}
over 4 years ago · Santiago Trujillo Relatório

0

When you use $('section').attr('id') that will always return the id of the first section, You need to use this inside of section in :

$('section').attr('id');

Should be :

$(this).attr('id');

So it will get the id of the current section, Also no need for the replace the both lines could be combined to :

$body.addClass($(this).attr('id') + '-active');

NOTE : You could always get the active section using just selector like :

$('section.active')

Hope this helps.

over 4 years ago · Santiago Trujillo Relatório

0

Actually $('section').attr('id') will give you the id attribute without # you can use it directly, you can see body class changed accordingly in the folowing Demo:

if ($('section').hasClass('active')) {
  $("body").addClass($('section').attr('id') + '-active');
} else {
  $("body").removeClass('home-active');
}

console.log($("body").attr('class'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body class="">
  <section id="home" class="active">
  </section>

  <section id="about" class="">
  </section>
</body>

Note:

Note that you are using $body which you haven't declared, use $("body") instead to refer document body.

over 4 years ago · Santiago Trujillo 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