Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

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 07-12-2010, 08:11 PM   PM User | #1
chrismcl
New to the CF scene

 
Join Date: Jul 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
chrismcl is an unknown quantity at this point
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.
chrismcl is offline   Reply With Quote
Old 07-12-2010, 08:18 PM   PM User | #2
A1ien51
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
A1ien51 will become famous soon enough
Look at the difference between


document.getElementById("yourspy").value

and

document.getElementById("answer1")

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 07-12-2010, 08:40 PM   PM User | #3
chrismcl
New to the CF scene

 
Join Date: Jul 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
chrismcl is an unknown quantity at this point
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.
chrismcl is offline   Reply With Quote
Old 07-12-2010, 08:50 PM   PM User | #4
chrismcl
New to the CF scene

 
Join Date: Jul 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
chrismcl is an unknown quantity at this point
sorry for the noobness. I get you now. It should be this yeah:

Code:
ansD.value = val3 * 0.7 + ansA.value;
chrismcl is offline   Reply With Quote
Old 07-12-2010, 08:55 PM   PM User | #5
tomws
Senior Coder

 
tomws's Avatar
 
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
tomws will become famous soon enoughtomws will become famous soon enough
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?
tomws is offline   Reply With Quote
Old 07-14-2010, 08:32 PM   PM User | #6
chrismcl
New to the CF scene

 
Join Date: Jul 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
chrismcl is an unknown quantity at this point
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?
chrismcl is offline   Reply With Quote
Old 07-14-2010, 08:52 PM   PM User | #7
tomws
Senior Coder

 
tomws's Avatar
 
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
tomws will become famous soon enoughtomws will become famous soon enough
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?
tomws 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 01:43 PM.


Advertisement
Log in to turn off these ads.