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

191
Visualizações
Set next element value based on clicked element value using jquery (or javascript)

I have two input, first is checkbox and the other is input hidden. I also have multiple input on 1 page

<input type="checkbox" class="comp-checkk" value="45164c0b-fc92-4f93-9c62-ea61336130eb">
<input type="hidden" class="comp-id" name="component_id[]" value="45164c0b-fc92-4f93-9c62-ea61336130eb"> 

<input type="checkbox" class="comp-checkk" value="45164c0b-fc92-4f93-9c62-ea61336130eb">
<input type="hidden" class="comp-id" name="component_id[]">

<input type="checkbox" class="comp-checkk" value="45164c0b-fc92-4f93-9c62-ea61336130eb">
<input type="hidden" class="comp-id" name="component_id[]">

I want to set the value of input with comp-id class using value from comp-checkk class value. And if the value of comp-id is not null, I want to set comp-id to null, else if the value of comp-id is null, I want to set comp-id value with value from comp-check

I've tried like this:

$(document).on('change','.comp-checkk',function(){
    if ($(this).next().val() != null) {
       $(this).next().val(null);
    } else {
       $(this).next().val($(this).val());
    }
});

It's working if the comp-id is not null, but when the value is null it's not working (nothing happened). I don't know what is wrong with my code.

I also made JsFiddle here

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

0

You have to check for both not null and also for not empty

if ($(this).next().val() != null && $(this).next().val() != '') {
   $(this).next().val(null);
} else {
   $(this).next().val($(this).val());
}
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