...

View Full Version : field_x should have the same value as field_y except...



Drieske1982
03-12-2004, 09:26 AM
Hi,
I have a javascript-form at:
http://www.aendekerk.be/beta/airco/bereken.html

I'm still working on it, people on Codingforums.com have helped me alot, and i hope you still do. :)

I have a formfield named "b1x1_totaal" and a formfield named "b1x1_tot_view". The first one contains a certain value (number) (depending on user input).

I want that the second one (b1x1_tot_view) always contains the same value as the first one (b1x1_totaal) except when the value of the first one is '0,00' (zero), then, the value of 'b1x1_tot_view' should be 'X'.

How do i do this?

Kor
03-12-2004, 09:56 AM
Try this:



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script>
function bla(){
d=document.forms[0];
if(parseFloat(d.b1x1_totaal.value) == 0){
d.b1x1_tot_view.value = 'X'
}
else {d.b1x1_tot_view.value = d.b1x1_totaal.value}
}
</script>
</head>

<body>
<form>
b1x1_totaal
<input name="b1x1_totaal" type="text" onkeyup="bla()"><br><br>
b1x1_tot_view<input name="b1x1_tot_view" type="text">
</form>
</body>
</html>

Drieske1982
03-12-2004, 11:01 AM
Thanks!!
This one works!


if(parseFloat(document.aForm.b1x1_totaal.value) == 0){
document.aForm.b1x1_tot_view.value = 'X'
}
else {document.aForm.b1x1_tot_view.value = document.aForm.b1x1_totaal.value}
if(parseFloat(document.aForm.b1x2_totaal.value) == 0){
document.aForm.b1x2_tot_view.value = 'X'
}
else {document.aForm.b1x2_tot_view.value = document.aForm.b1x2_totaal.value}
if(parseFloat(document.aForm.b1x3_totaal.value) == 0){
document.aForm.b1x3_tot_view.value = 'X'
}
else {document.aForm.b1x3_tot_view.value = document.aForm.b1x3_totaal.value}
if(parseFloat(document.aForm.b1x4_totaal.value) == 0){
document.aForm.b1x4_tot_view.value = 'X'
}
else {document.aForm.b1x4_tot_view.value = document.aForm.b1x4_totaal.value}

Kor
03-12-2004, 11:43 AM
Better try to keep the code shorter by using loops and shortcut notation:



for(i=1;i<5;i++){
d = document.aForm;
if(parseFloat(d.elements['b1x'+i+'_totaal'].value) == 0){
d.elements['b1x'+i+'_tot_view'].value = 'X'
}
else d.elements['b1x'+i+'_tot_view'].value = d.elements['b1x'+i+'_totaal'].value}
}

Kor
03-12-2004, 11:54 AM
an error occured(a braqett missed)

here's the good one



for(i=1;i<5;i++){
d = document.aForm;
if(parseFloat(d.elements['b1x'+i+'_totaal'].value) == 0){
d.elements['b1x'+i+'_tot_view'].value = 'X'
}
else {d.elements['b1x'+i+'_tot_view'].value = d.elements['b1x'+i+'_totaal'].value}
}


Whenever you have large number of similar objects on page, try using loops and any incremental way to keep the code as short as possible

Drieske1982
03-12-2004, 12:31 PM
Hey,
Thanks for the tip!!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum