He estado creando un sitio web JS donde te dice tu edad en días. Escribí el código para que, si naciste antes de 1990, el sitio web diga "Vaya, eres viejo". Diseñé ese texto aparte del otro texto pero la salida estaba en otra línea. Quiero traer ambos textos en la misma línea. No sé cómo hacerlo, por lo que su ayuda es muy apreciada. Aquí está el código JS:
function ageInDays() { // variables var birthYear = prompt("What Year Were You Born In?"); var ageInDayss = (2021 - birthYear) * 365; //text if (birthYear>1990) { 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 { var h1 = document.createElement('h1'); var h2 = document.createElement('h2'); 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); } }¿Cómo traigo la salida "else" en una línea?
¿Necesita ser explícitamente h1 y h2? Son elementos de bloque por defecto y no es realmente una buena práctica usar un elemento para otra cosa (usarlo en línea) para lo que fue creado.
Recomiendo usar span
... var text1 = document.createElement('span'); var text2 = document.createElement('span'); ...y estilizarlo en consecuencia.