Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-30-2012, 02:32 PM   PM User | #1
Peeyush
Regular Coder

 
Join Date: Apr 2012
Posts: 104
Thanks: 27
Thanked 2 Times in 2 Posts
Peeyush is an unknown quantity at this point
Smile A little DOM error, misinterpreting constant (number) with strings...

Code:
Code:
function calc()
{
	var calc = document.getElementById("main");
	var x = calc.num1.value;
	var y = calc.num2.value;
	var sum = x + y;
	document.getElementById("result").innerHTML = sum ;
}
When I enter value of x as 1 and of y as 2, sum i get is 12. why is JS misinterpreting it as strings?
Peeyush is offline   Reply With Quote
Old 04-30-2012, 02:51 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
You are rather misinterpreting it as numbers :-)

The value of HTML input fields is always a string. You have to convert it to whatever data type you need
Code:
var x = parseFloat(calc.num1.value);
var y = parseFload(calc.num2.value);
var sum = x + y;

// now sum will be numeric
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
Peeyush (04-30-2012)
Old 04-30-2012, 02:57 PM   PM User | #3
Peeyush
Regular Coder

 
Join Date: Apr 2012
Posts: 104
Thanks: 27
Thanked 2 Times in 2 Posts
Peeyush is an unknown quantity at this point
Thumbs up Thanks :)

ThankU ThankU ThankU very much. if i had 1000 accounts on CF, i would have thanked you 1000 times and more!! can you please tell me where did you syudy this from? i am a self taught newbie , i study from w3schools.com
Peeyush is offline   Reply With Quote
Old 04-30-2012, 03:41 PM   PM User | #4
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,387
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
Quote:
Originally Posted by Peeyush View Post
...snip... can you please tell me where did you syudy this from? i am a self taught newbie , i study from w3schools.com
Acually, you should have learned this from W2schools; page http://www.w3schools.com/html/html_forms.asp

Quote:
<input type="text" /> defines a one-line input field that a user can enter text into:
<input type="text" name="firstname" />
There is no type="numeric".
Quote:
I don't bring this up to make you feel small. We all need help from time to time, because we forget. We only remember 10% of what we learn. I'm just saying the fault is in our brain retention not where we learned it. I like W3schools
sunfighter is offline   Reply With Quote
Old 04-30-2012, 08:05 PM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,455
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Peeyush View Post
i study from w3schools.com
That is one of the worst places to learn JavaScript - most of the pages teach how to write JavaScript for netscape 4 and earlier and only the new commands introduced since 2005 are covered without the pages covering the now obsolete commands being rewritten.

Most of the rest of the site isn't too dated as the many other languages the two owners of that site cover haven't changed anywhere near as rapidly as JavaScript has.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 05-01-2012, 10:06 AM   PM User | #6
Peeyush
Regular Coder

 
Join Date: Apr 2012
Posts: 104
Thanks: 27
Thanked 2 Times in 2 Posts
Peeyush is an unknown quantity at this point
@sunfighter thanks for pointing out my mistake :P i never really practiced per chapter, i just read it all and then moved to next chapter, thinking i will use all that i had learned when i start making a real practice, but as they say, practice makes a man perfect, also i had tried type = number method, it wasn't working still, and i would like to thank devnull69 again, coz his method worked properly, i aint really good at DOM....

@felgall thanks for telling me the other website to learn JavaScript From, i greatly appreciate it
Peeyush is offline   Reply With Quote
Old 05-11-2012, 10:44 AM   PM User | #7
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
Quote:
Originally Posted by sunfighter View Post
There is no type="numeric".
Actually there is type="number". But in HTML5, along with a dozen of new types. See also:
http://www.456bereastreet.com/archiv...5_input_types/
Unfortunately, not all of them are cross-browser, yet. See also:
http://www.quirksmode.org/html5/inputs.html

But those types are rather used to ease the way user fills the fields (most of the time on the smartphones), not to change the nature of the value of the INPUT element, which is always a primitive String.
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:03 PM.


Advertisement
Log in to turn off these ads.