Tengo un pequeño fragmento de código donde necesito ingresar el tipo de valor. En este momento, solo obtengo el valor ingresado en el cuadro, pero no el tipo. ¿Me puede ayudar alguien?
<p id="res">Value</p><input type="text" id="fname" name="fname"><br><br> <script> document.getElementById('fname').addEventListener('input', function(){ document.getElementById('res').textContent= this.value; }); </script>Debe usar la palabra clave typeof , y si desea probar el number frente a la string , debe hacer un tipo de verificación usando isNaN y Number
Cambié tu función a función de flecha.
document.getElementById('fname').addEventListener('input', e => { const value = e.currentTarget.value; const checkType = isNaN(Number(value)) ? value : Number(value) document.getElementById('res').textContent = typeof checkType }) <p id="res">Value</p><input type="text" id="fname" name="fname"><br><br>Así que typeof no te va a ayudar aquí. porque la entrada es tipo = "texto", todo lo que se escriba se convertirá en una cadena. exp: algunos tipos a 4, se le dará a js como "4". Supongo que no te importa el tipo en sí y solo quieres saber si escribieron un número. si ese es el caso, use la función isNaN que devolverá falso si un valor se puede convertir en un número
isNaN("4") //false isNaN("hello") //true
PD: NaN significa no un número