FOROS PROFESIONALES
WINDEV
,
WEBDEV
y
WINDEV Mobile
Inicio
|
Mensajes recientes
|
Conéctese...
|
Desconectar
|
Español
Inicio
→
WEBDEV 2024
→
JavaScript String to Number Conversion in HTML: Seeking Clarification
JavaScript String to Number Conversion in HTML: Seeking Clarification
Iniciado por Mobiwann, 31,ago. 2023 13:52 - No hay respuesta
Conéctese…
Mobiwann
#1
Miembro registrado
6 mensajes
Publicado el 31,agosto 2023 - 13:52
I'm working on a small HTML and JavaScript project, and I'm encountering an issue with converting strings to numbers. I have an input field in my HTML where users can enter a number as a string. I'm using JavaScript to convert this string to a number and perform calculations, but I'm not getting the expected results.
Here's my HTML and JavaScript code:
<
!
DOCTYPE html
>
<
html
>
<
head
>
<
title
>
String to Number Conversion
<
/
title
>
<
/
head
>
<
body
>
<
input type
=
"text"
id
=
"numberInput"
placeholder
=
"Enter a number"
>
<
button onclick
=
"calculate()"
>
Calculate
<
/
button
>
<
p id
=
"result"
>
<
/p>
<
script
>
function calculate() {
let input = document.getElementById("numberInput").value;
let convertedNumber = Number(input);
let result = convertedNumber + 10;
document.getElementById("result").textContent = "Result: " + result;
}
<
/
script
>
<
/
body
>
<
/
html
>
When I enter a number into the input field and click the "Calculate" button, the result is not as expected. For instance, if I enter "5", the result displayed is "510" instead of "15".
Could someone explain why this is happening? Am I missing something in my code? How can I ensure that the string is correctly converted to a number before performing calculations? Any insights or code modifications would be greatly appreciated. Thank you!
Informar
0
0
→ Volver a WEBDEV 2024
WINDEV 2024
WEBDEV 2024
WINDEV Mobile 2024
WINDEV (versiones precedentes)
Français
English
Español
Portuguesa
Fermer cette fenêtre
Búsqueda Tipo
Sólo temas
Todos los mensajes
Periodo Buscar
Cualquier fecha
Última hora
Última 24 horas
Última semana
Último mes
Último año
Cancelar
Vista previa del mensaje
Añadiendo una imagen
Importar una imagen desde una URL
Enviar una imagen desde un archivo de su disco
Arrastrar un archivo o click en "Examinar..."
o
Cancelar
0%
WLanguage
SQL
XML, HTML
JAVA, Javascript
Texto