Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

75
Vistas
document.querySelector .value return value

I have a web page with a few radio button selections. Not all of the radio button options have to be selected. To get the value of each of the Radio Button options I am using this:

var hotend = document.querySelector('input[name="hotend"]:checked').value;

I would think that this should set hotend to null? But it seems like this crashes the webpage when there hasn't been a radio button selected in the group because the rest of function does not run. How do I capture this and process the rest of the function?

Here is the entire code:

<!DOCTYPE html>
<html>
 
<head>
<title>test</title>

</head>
 
<body>
    <h1>parts</h1>
    <label for="printer">Choose a part:</label>
    <select id="printer" name="printer">
        <option value="none">Select Printer</option>
        <option value="CR_10">CR-10</option>
        
        
    </select>
    <br />   
    Choose a hotend:
    <div class="flex-container">   
        <div>            
            <input type="radio" id="creality_OEM" name="hotend" value="Creality_OEM">
            <label for="creality_OEM">Creality OEM</label>
        </div>
        <div>            
            <input type="radio" id="micro_swiss" name="hotend" value="Micro_Swiss">
            <label for="micro_swiss">Micro Swiss</label>
        </div>
        
    </div>
   
<button class="button" onclick="createParts();" >Generate Parts List</button> 
<div id="parts_list">
test    
</div>
<div id="test">
 something  
</div>
<script>

function createParts(){
    var partsList="";
    var errorList="";
       
    const printer = document.getElementById('printer').value;
    let nullTest = document.querySelector('input[name="hotend"]:checked');
    const hotend= nullTest.value ?? "";
        
    document.getElementById('parts_list').innerHTML=partsList;
    document.getElementById('test').innerHTML="something";
    
}

</script>
</body>

</html>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Your code "crashes" because you're trying to access the property value on a null˙object.

Because document.querySelector('input[name="hotend"]:checked') returns null when there's no radio buttons selected.

You're basically doing this when no radio button is selected: null.value.

Try this:

const hotendElement = document.querySelector('input[name="hotend"]:checked');
const hotend = hotendElement.value ?? "No hotend selected";

Also, do not use var. Use const and let.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda