FORUMS PROFESSIONNELS
WINDEV
,
WEBDEV
et
WINDEV Mobile
Accueil
|
Messages récents
|
Connexion
|
Déconnexion
|
Français
Accueil
→
WEBDEV 2024
→
JavaScript String to Number Conversion in HTML: Seeking Clarification
JavaScript String to Number Conversion in HTML: Seeking Clarification
Débuté par Mobiwann, 31 aoû. 2023 13:52 - Aucune réponse
Connectez-vous…
Mobiwann
#1
Membre enregistré
6 messages
Posté le 31 août 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!
Signaler
0
0
→ Revenir à WEBDEV 2024
WINDEV 2024
WEBDEV 2024
WINDEV Mobile 2024
WINDEV (précédentes versions)
Français
English
Español
Portuguesa
Fermer cette fenêtre
Type de recherche
Uniquement les sujets
Tous les messages
Période de recherche
Date indifférente
Moins d'une heure
Moins de 24 heures
Moins d'une semaine
Moins d'un mois
Moins d'un an
Annuler
Aperçu de votre message
Ajouter une image
Importer une image depuis une URL
Envoyer une image depuis un fichier de votre disque
Déposez ici un fichier ou cliquez sur "Parcourir..."
ou
Annuler
0%
WLangage
SQL
XML, HTML
JAVA, Javascript
Texte