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

98
Visualizações
Checkboxes to replace value

Need some help. New to JS and HTML. I'm trying to get a checkbox to change any result from null to A.

A, null, A, A or any other combination should be the result

The result keeps showing only null, null, null, null and in html instead of the result box

https://jsfiddle.net/uw21myj8/

var k1 = 'null';
var k2 = 'null';
var k3 = 'null';
var k4 = 'null';

$(function(){
    $('#checkbox').change(function() {
      $("#text").val($(this).is(':checked') ? "A" : "B");
      k1 = 'C5';
    });
});

$(function(){
    $('#checkbox1').change(function() {
      $("#text1").val($(this).is(':checked') ? "A" : "B");
      k2 = 'C5';
    });
});
$(function(){
    $('#checkbox2').change(function() {
      $("#text2").val($(this).is(':checked') ? "A" : "B");
      k3 = 'C5';      
    });
});
$(function(){
    $('#checkbox3').change(function() {
        $("#text3").val($(this).is(':checked') ? "A" : "B");
        k4 = 'C5';     
    });
});


var results = (k1 + ', ' + k2 + ', ' + k3 + ', ' + k4 + ', ');

function getresults(){
       $("#result").text(results);
  console.log(results);
     };

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

0

Your JS needs some adjustments:

var k1 = 'null';
var k2 = 'null';
var k3 = 'null';
var k4 = 'null';

$(function(){
    $('#checkbox').change(function() {
      $("#text").val($(this).is(':checked') ? "A" : "B");
      k1 = $(this).is(':checked') ? "A" : "null";
    });
});

$(function(){
    $('#checkbox1').change(function() {
      $("#text1").val($(this).is(':checked') ? "A" : "B");
      k2 = $(this).is(':checked') ? "A" : "null";
    });
});
$(function(){
    $('#checkbox2').change(function() {
      $("#text2").val($(this).is(':checked') ? "A" : "B");
      k3 = $(this).is(':checked') ? "A" : "null";      
    });
});
$(function(){
    $('#checkbox3').change(function() {
        $("#text3").val($(this).is(':checked') ? "A" : "B");
        k4 = $(this).is(':checked') ? "A" : "null";     
    });
});


function getresults() {
  var results = (k1 + ', ' + k2 + ', ' + k3 + ', ' + k4 + ', ');
  $("#result").text(results);
  console.log(results);
};
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