PROFESSIONAL NEWSGROUPS
WINDEV
,
WEBDEV
and
WINDEV Mobile
Home
|
Recent messages
|
Connect
|
Sign out
|
English
Home
→
WEBDEV 2024
→
JavaScript String to Number Conversion in HTML: Seeking Clarification
JavaScript String to Number Conversion in HTML: Seeking Clarification
Started by Mobiwann, Aug., 31 2023 1:52 PM - No answer
Connect yourself…
Mobiwann
#1
Registered member
6 messages
Posted on August, 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!
Report
0
0
→ Go back to WEBDEV 2024
WINDEV 2024
WEBDEV 2024
WINDEV Mobile 2024
WINDEV (earlier versions)
Français
English
Español
Portuguesa
Close this window
Search type
Only topics
All the messages
Search period
Any time
Past hour
Past 24 hours
Past week
Past month
Past year
Cancel
Preview of your message
Adding an image
Import an image from a URL
Send an image from a file of your disk
Drop a file or click "Browse..."
or
Cancel
0%
WLanguage
SQL
XML, HTML
JAVA, Javascript
Text