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

133
Visualizações
jQuery add class to fieldset if input is have error class

How can I add "error" class to the fieldset(s) too if their input's are have "error" class?

I'm trying to achieve this as the following, but it's adding the "error" class to every fieldsets no matter if the input is have the "error" class or not.

$('#submit').click(function(e) {
    e.preventDefault();
    $('input').each(function() {
        if($(this).hasClass("error")){
            $("fieldset").addClass("error");
        }
    });
});
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="form-holder">
    <fieldset>
        <legend>First name</legend>
        <input type="text" class="form-control" name="firstname" placeholder="First name" required>
    </fieldset>
</div>

<div class="form-holder">
    <fieldset>
        <legend>Last name</legend>
        <input type="text" class="form-control error" name="lastname" placeholder="Last name" required>
    </fieldset>
</div>

<div class="form-holder">
    <fieldset>
        <legend>Email</legend>
        <input type="email" class="form-control error" name="email" placeholder="Email" required>
    </fieldset>
</div>

<input type="submit" id="submit" value="Send">

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

0

The code is doing what you are telling it to. When you say $("fieldset").addClass("error"); it means to add the error class to all fieldset elements.

What you need to do is find the fieldset that is the parent of the input which has the error class. In your example this would be:

$('#submit').click(function(e) {
    e.preventDefault();
    $('input').each(function() {
        if($(this).hasClass("error")){
            $(this).closest("fieldset").addClass("error");
        }
    });
});

The closest() method will travel up the DOM looking for the first element which matches the selector. So it should find the closest fieldset parent to the input.

about 4 years ago · Juan Pablo Isaza Relatório

0

Try targeting the parent fieldset explicitly by using the parent selector. (And also cache the jQuery'd this var.)

$('#submit').click(function(e) {
    e.preventDefault();

    $('input').each(function() {
        var $this = $(this);
        if ($this.hasClass("error")) {
            $this.parent("fieldset").addClass("error");
        }
    });
});
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="form-holder">
    <fieldset>
        <legend>First name</legend>
        <input type="text" class="form-control" name="firstname" placeholder="First name" required>
    </fieldset>
</div>

<div class="form-holder">
    <fieldset>
        <legend>Last name</legend>
        <input type="text" class="form-control error" name="lastname" placeholder="Last name" required>
    </fieldset>
</div>

<div class="form-holder">
    <fieldset>
        <legend>Email</legend>
        <input type="email" class="form-control error" name="email" placeholder="Email" required>
    </fieldset>
</div>

<input type="submit" id="submit" value="Send">

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