PDA

View Full Version : Help with javascript


chrismcl
07-12-2010, 08:11 PM
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.

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

A1ien51
07-12-2010, 08:18 PM
Look at the difference between


document.getElementById("yourspy").value

and

document.getElementById("answer1")

Eric

chrismcl
07-12-2010, 08:40 PM
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
07-12-2010, 08:50 PM
sorry for the noobness. I get you now. It should be this yeah:

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

tomws
07-12-2010, 08:55 PM
A1ien51's answer was on-track, but perhaps slightly more cryptic than I would have been. :)

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

It's an HTML element.

Now, look at where you use it next:
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! :p

chrismcl
07-14-2010, 08:32 PM
Hey folks. I still can't get it to work right. Here is what I have just now:

<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?

tomws
07-14-2010, 08:52 PM
The '+' operator is also the concatenation operation in addition (ha!) to being addition. Try this workaround and see if it does what you want:
ansC.value = val3 * 0.7 + val3 * 1;