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
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with javascript

    Hi there folks,

    I am kind of new to javascript and can't seem to get what I am wanting. Basically here is the code I have so far.

    Code:
    <script language="javascript">
                    function addNumbers()
                    {
                            var val1 = parseInt(document.getElementById("yourspy").value);
                            var val2 = parseInt(document.getElementById("numspy").value);
                            var val3 = parseInt(document.getElementById("enmyspypr").value);
                            var ansA = document.getElementById("answer1");
                            var ansB = document.getElementById("answer2");
                            var ansC = document.getElementById("answer3");
                            var ansD = document.getElementById("answer4");
                            ansA.value = val1 / val2;
                            ansB.value = val3 * 0.7;
                            ansC.value = val3 * 0.7 + val3;
                            ansD.value = val3 * 0.7 + ansA;
                    }
            </script>
    The problem I am having seems to be that ansA is not able to be used other that displaying in a text area. Can anyone help me make ansA available for the formula to use? Many thanks in advance.

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Look at the difference between


    document.getElementById("yourspy").value

    and

    document.getElementById("answer1")

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by A1ien51 View Post
    Look at the difference between


    document.getElementById("yourspy").value

    and

    document.getElementById("answer1")

    Eric
    Not getting you. Please bear in mind this is my first shot at javascript and only have the basics from what I can find on the net. Are you saying that I should put.value at the end of the answer1 line? Sorry for being such a noob.

  • #4
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry for the noobness. I get you now. It should be this yeah:

    Code:
    ansD.value = val3 * 0.7 + ansA.value;

  • #5
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    A1ien51's answer was on-track, but perhaps slightly more cryptic than I would have been.

    Look where you're defining ansA:
    Code:
    var ansA = document.getElementById("answer1");
    Then answer: what is ansA?

    It's an HTML element.

    Now, look at where you use it next:
    Code:
    ansD.value = val3 * 0.7 + ansA;
    This assignment doesn't make sense. Read it:
    To the element ansD, assign a value of val3 * 0.7 + an HTML element.

    See it now?

    EDIT: Doh! You beat me in answering your own post!
    Are you a Help Vampire?

  • #6
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey folks. I still can't get it to work right. Here is what I have just now:

    Code:
    <script language="javascript">
                    function addNumbers()
                    {
                            var val1 = parseInt(document.getElementById("yourspy").value);
                            var val2 = parseInt(document.getElementById("numspy").value);
                            var val3 = parseInt(document.getElementById("enmyspypr").value);
                            var ansA = document.getElementById("answer1");
                            var ansB = document.getElementById("answer2");
                            var ansC = document.getElementById("answer3");
                            var ansD = document.getElementById("answer4");
                            ansA.value = val1 / val2;
                            ansB.value = val3 * 0.7;
                            ansC.value = val3 * 0.7 + val3;
                            ansD.value = ansC.value / ansA.value;
                    }
            </script>
    The problem is ansC and ansD. ansC appears to do the first bit but then just put the 2 number together and not actually add them. I am like completely stuck. I am begining to hate javascript. I can't seem to get my head around it. any ideas?

  • #7
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    The '+' operator is also the concatenation operation in addition (ha!) to being addition. Try this workaround and see if it does what you want:
    Code:
    ansC.value = val3 * 0.7 + val3 * 1;
    Are you a Help Vampire?


  •  

    Posting Permissions

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