/    Sign up×
Community /Pin to ProfileBookmark

Get values from several textfields onChange

Hi – first post here.

I have a scenario with 3 x 12 textfields and looking for a javascript that could do the trick.

What I’m looking for is a smart way of extracting the values of the first three textfields and then multiply them. This will be done for all 12 rows.
I then would like to add the result from the 12 rows together to create a final value that will be presented in a seperate textfield.

[B]row1=value1*value2*value3;
row2 =value1*value2*value4; and so on….untill row12.
then row1+row2+…row12[/B]

To show you what I mean I’ve created a small page with 3 x 2 fields at this link: [URL=”http://www.drink2drive.com/dev/html/test1.php”]http://www.drink2drive.com/dev/html/test1.php[/URL]
The idea is then to have 12 rows and not two.
Also – it should call for the function onChange.

I guess combind all the values in three seperate [B]arrays[/B], multiply and then add the values together could do the trick, but so far I havn’t manage to do this.
Maybe a for(i<12)-code could help.

So – I really hope that some of you have a smart way of doing this. It would help a lot and make me very happy!

Kind regards,
Pixy

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERJan 09.2010 — First, welcome to the forum. ?

Second, try this...?
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Drinking Scores&lt;/title&gt;
&lt;script type="text/javascript"&gt;
function recalcInfo() {
var sel;
var tot = 0;
var sum = 0;
for (r=0; r&lt;12; r++) {
id="r"+r+"c";
sum = Number(document.getElementById(id+'0').value) * Number(document.getElementById(id+'1').value);
document.getElementById(id+'2').value = sum;
tot += sum;
}
document.getElementById('Overall').value = tot;
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;script type="text/javascript"&gt;
var id = '';
var str = '&lt;table border="0"&gt;';
for (r=0; r&lt;12; r++) {
str += '&lt;tr&gt;';
for (c=0; c&lt;3; c++) {
id="r"+r+"c"+c;
str += '&lt;td style="width:100px"&gt;&lt;input type="text" id="'+id+'" onchange="recalcInfo()"';
str += ' size="5" value="0"&gt;&lt;/td&gt;';
}
str += '&lt;/tr&gt;';
}
str += '&lt;tr&gt;&lt;td colspan="2"&gt;&amp;nbsp;&lt;/td&gt;';
str += '&lt;td&gt;&lt;input type="text" id="Overall" size="5" value="0"&gt;&lt;/td&gt;&lt;/tr&gt;';
str += '&lt;/table&gt;'
document.write(str); <br/>
&lt;/script&gt;
&lt;/body&gt;
&lt;html&gt;

You can pretty it up with you graphics and descriptions as needed. :p

Good Luck!

?
Copy linkTweet thisAlerts:
@PixyauthorJan 10.2010 — Thank you JMRKER. I will give it a try now to see if I can adopt your code to my needs.

I'll get back to you and give you the results later today.

Cheers

Pixy
Copy linkTweet thisAlerts:
@PixyauthorJan 10.2010 — It seems to work the way I want to, but I ran across another problem.

I'm using a DropDownList and I can only seem to get the Index-number of the selected value ?

Is there a short code-snipet that could possibly give me the selected value.

Here's my page: http://www.drink2drive.com/dev/html/test2.php

[CODE]document.getElementById(id+'2').value[/CODE] would not work since it does not know what value to get, I guess.

Any help would be greatly appreciated.

/Pixy
Copy linkTweet thisAlerts:
@JMRKERJan 10.2010 — I am unclear as to what you want.

How is the drop-down supposed to be associated with the multiplication of the 1st two numbers?
Copy linkTweet thisAlerts:
@PixyauthorJan 10.2010 — I managed to get the value myself.
[CODE]var sum2 = document.getElementById(id+'2').options[document.getElementById(id+'2').selectedIndex].text;
var sum3 = parseFloat(sum2);[/CODE]


Parsed the text to a Float.

Thank you anyway ?
Copy linkTweet thisAlerts:
@JMRKERJan 10.2010 — Your most welcome

Glad I could help,

but you might want to make your problems clearer in the future for faster responses.

Good Luck!

?
Copy linkTweet thisAlerts:
@PixyauthorJan 15.2010 — Thank you webdevelopers (and spec. JMKER)!

I've now finished my webpage: http://www.drink2drive.com

It couldn't have been done without your combind knowledge.

/PIXY
×

Success!

Help @Pixy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...