GRUPOS DE DISCUSSÃO PROFISSIONAL
WINDEV
,
WEBDEV
e
WINDEV Mobile
Inicio
|
Mensagens recentes
|
Conecte-se...
|
Disconnect
|
Português
Inicio
→
WEBDEV 2024
→
JavaScript String to Number Conversion in HTML: Seeking Clarification
JavaScript String to Number Conversion in HTML: Seeking Clarification
Iniciado por Mobiwann, ago., 31 2023 1:52 PM - Sem resposta
Conecte-se…
Mobiwann
#1
Membro registado
6 mensagems
Publicado em agosto, 31 2023 - 1:52 PM
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!
Denunciar
0
0
→ Voltar para WEBDEV 2024
WINDEV 2024
WEBDEV 2024
WINDEV Mobile 2024
WINDEV (versões anteriores)
Français
English
Español
Portuguesa
Fermer cette fenêtre
Tipo de Pesquisa
Apenas tópicos
Todas as mensagens
Período de pesquisa
Qualquer momento
Última hora
Últimas 24 horas
Semana passada
Mês passado
Ano passado
Cancelar
Pré-visualização da sua mensagem
Adicionar imagem
Importar uma imagem de uma URL
Enviar uma imagem a partir do seu disco
Deixe um ficheiro ou clique em "Procurar ..."
ou
Cancelar
0%
WLanguage
SQL
XML, HTML
JAVA, Javascript
Texto