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

207
Vistas
How to allow javascript form to skip questions without producing text which assumes an answer?

I have created a page with a form for people to complete, which when completed produces a text file of the person's answers.

However I want people to have the option of skipping a question if they don't want to answer it.

The problem with the code below is that if you skip a question it still produces the preceding/following text and spaces.

i.e. if a person doesn't want to answer the question about their age, the below code will still produce text which reads "Age:[space][space]" in the text file.

If someone skips a question how to I stop the text file still producing the above? Ideally I want it to be completely ignored in the text file if the question hasn't been answered.

    // Get the data from each element on the form.
    const name = document.getElementById('txtName');
    const age = document.getElementById('txtAge');
    const email = document.getElementById('txtEmail');
    const country = document.getElementById('selCountry');
    const msg = document.getElementById('msg');
    
    // This variable stores all the data.
    let data = 
        '\r Name: ' + name.value + ' \r\n ' + 
        'Age: ' +age.value + ' \r\n ' + 
        'Email: ' + email.value + ' \r\n ' + 
        'Country: ' + country.value + ' \r\n ' + 
        'Message: ' + msg.value;
    
    // Convert the text to BLOB.
    const textToBLOB = new Blob([data], { type: 'text/plain' });
    const sFileName = 'formData.txt';      // The file to save the data.

    let newLink = document.createElement("a");
    newLink.download = sFileName;

    if (window.webkitURL != null) {
        newLink.href = window.webkitURL.createObjectURL(textToBLOB);
    }
    else {
        newLink.href = window.URL.createObjectURL(textToBLOB);
        newLink.style.display = "none";
        document.body.appendChild(newLink);
    }

    newLink.click(); 
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Create a simple function like this

const fn = (pre, v, post) => v.length? `${pre || ''} ${v} ${post || ''}`;

use it like this

let data = 
        fn('\r Name: ', name.value, ' \r\n ') +
        fn('Age: ', age.value, ' \r\n ') + 
        .... etc
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