Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Apr 2012
    Posts
    104
    Thanks
    27
    Thanked 2 Times in 2 Posts

    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?

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    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

  • Users who have thanked devnull69 for this post:

    Peeyush (04-30-2012)

  • #3
    Regular Coder
    Join Date
    Apr 2012
    Posts
    104
    Thanks
    27
    Thanked 2 Times in 2 Posts

    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

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,721
    Thanks
    25
    Thanked 662 Times in 661 Posts
    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

    <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".
    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

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    Regular Coder
    Join Date
    Apr 2012
    Posts
    104
    Thanks
    27
    Thanked 2 Times in 2 Posts
    @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

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •