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

147
Vistas
PHP/JS only outputting content for 1 second

I am creating a website where if you input the year you were born in, the website will output your age in days. The code works for the most part. When I press the submit button, the age outputs for 1 second and then leaves the screen. I want the text to stay there. Here's my JS Code:

function ageInDays() {
    // variables
    var birthYear = <?php echo $_GET["name"];?>;
    var thisYear = new Date();
    var ageInDayss = (thisYear.getFullYear() - birthYear) * 365;


    //text
    if (isNaN(birthYear) == false) {
        if (birthYear<=1920 || birthYear>=2022) {  
            var h1 = document.createElement('h1');
            var textAnswer = document.createTextNode("Invalid Input. Please Try 
            Again.");
            h1.setAttribute('id', 'ageInDays');
            h1.appendChild(textAnswer);
            document.getElementById('flex-box-result').appendChild(h1);
    }
    
    else if (birthYear>1990 && birthYear<2021) {
        var h1 = document.createElement('h1');
        var textAnswer = document.createTextNode("You are " + ageInDayss + " days 
        old.");
        h1.setAttribute('id', 'ageInDays');
        h1.appendChild(textAnswer);
        document.getElementById('flex-box-result').appendChild(h1);

    } 


    else if (birthYear>1920 && birthYear<1990) {
        var h1 = document.createElement('span');
        var h2 = document.createElement('span');
        var textAnswer = document.createTextNode("You are " + ageInDayss + " days 
        old.");
        var textAnswer1 = document.createTextNode(" Yikes... that's old.");
        h1.setAttribute('id', 'ageInDays');
        h2.setAttribute('id', 'yikes');
        h1.appendChild(textAnswer);
        h2.appendChild(textAnswer1);
        document.getElementById('flex-box-result').appendChild(h1).appendChild(h2);
    }

}else{
    var h1 = document.createElement('h1');
    var textAnswer = document.createTextNode("Invalid Input. Please Try Again.");
    h1.setAttribute('id', 'ageInDays');
    h1.appendChild(textAnswer);
    document.getElementById('flex-box-result').appendChild(h1);
 }
}

When I run the code, the text only outputs for 1 second. How can I fix this?

about 4 years ago · Juan Pablo Isaza
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