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 9 of 9

Thread: javascript help

  1. #1
    New to the CF scene
    Join Date
    May 2014
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Post javascript help

    Hi members,

    Please help me out from this case;
    Here is that, actually my js onchange is not firing , showing NaN error.
    here is

    <td width="498"><input class="xyz" type="text" name="LT" size="10" maxlength="10" onFocus="FN(15)" onchange="changedef()" ></td>
    <td width="498"><input class="xyz" type="text" name="def" size="10" maxlength="10" onFocus="FN(16)" onkeypress="changeabc()"></td>
    <td width="498"><input class="xyz" type="text" name="abc" size="10" onFocus="FN(17)" maxlength="10" ></td>

    should fire at onkeypress="changeabc()", but it's not working showing NaN
    P.s. there is no submit button on this form,as it should change values by onchange or by any other way.
    function changeabc(){

    function changeabc(){
    gh= parseInt(document.forms[0].gh.value);
    def= parseInt(document.forms[0].def.value);
    TLP = parseInt( def * gh * 12 );
    document.forms[0].abc.value = parseInt(abc);
    }

    Highly appreciated if anybody could please help.

    Thanq
    ravi

  • #2
    New to the CF scene
    Join Date
    May 2014
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts
    please help me...thanq

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,105
    Thanks
    23
    Thanked 594 Times in 593 Posts
    function changeabc() does fire onkeypress in the second input box. Maybe the parseInt() is the culprit read about it here https://developer.mozilla.org/en-US/...jects/parseInt
    I like using Number() instead. https://developer.mozilla.org/en-US/...Objects/Number

    Then again it might be what the document.forms[0].--- etc contains, don't know not given in your code. Easy to check if that's the error by using the console or an alert.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    ravi907 (05-15-2014)

  • #4
    New to the CF scene
    Join Date
    May 2014
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts
    document.forms[0].gh.value and document.forms[0].def.value contains input values taken from the form..

    stores the values in gh and def ..therefore calculates the value and stores in abc..but still getting Nan
    gh= parseInt(document.forms[0].gh.value);
    def= parseInt(document.forms[0].def.value);
    abc = parseInt( def * gh * 12 );
    document.forms[0].abc.value = parseInt(abc);


    Thanks for your help sunfighter

  • #5
    New to the CF scene
    Join Date
    May 2014
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts
    when trying to get abc = parseInt( def * gh * 12 );
    am getting NaN error...please help

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,924
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by ravi907 View Post
    when trying to get abc = parseInt( def * gh * 12 );
    am getting NaN error...please help
    Use your error console or an alert to inspect the values of the variables. Note that it is a bad idea to assign the same name to an HTML element and a Javascript variable.

    Using document.forms[0] is also a bad idea as it ties the JavaScript directly to the HTML so that adding another form to the page earlier (such as if you decide to add a search box into the header of each page) will instantly break your JavaScript. Prefer to use document.getElementById("formid").

    It is also a bad idea to use meaningless variable names such as abc and def which give no clue as to what they are supposed to rerpresent.

    Javascript variables should be defined using the var keyword.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 05-14-2014 at 07:03 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    ravi907 (05-15-2014)

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,633
    Thanks
    0
    Thanked 649 Times in 639 Posts
    You left out the number base you are trying to convert from in the parseInt()

    if the number is already base 10 then you should be using Number() instead.
    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.

  • #8
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,105
    Thanks
    23
    Thanked 594 Times in 593 Posts
    If you check it, I did say to use the console to see what the def and gh variables were and to use Number() instead of parseInt() in his JS. Obviously he didn't do it and I don't know what else to tell ya.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #9
    New to the CF scene
    Join Date
    May 2014
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts
    TQ sunfighter & Philip M...highly appreciated


  •  

    Posting Permissions

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