...

View Full Version : textfield divided into textfield = a third field



Hrub
03-10-2004, 08:27 PM
New.. where should I begin? I have a form that has 2 text fields that need to be divided by each other once a person enters the amounts and the result to be placed in a third text field?

JAVAEOC
03-11-2004, 12:17 AM
why dont u alert the value, and make it easy on urselve :D

...

sad69
03-11-2004, 01:25 AM
It would be better to see your code, but anyway...



...
<script>
function divideFields() {
txt3.value = txt1.value / txt2.value;
}
</script>

<input type="text" id="txt1" name="txt1"/>
/
<input type="text" id="txt2" name="txt2"/>
=
<input type="text" id="txt3" name="txt3"/>
...


I think that should do it.

Sadiq.

Hrub
03-11-2004, 04:16 PM
Sorry, this is what I have. I cant understand how to get it to caculate...

<script language="JavaScript" type="text/JavaScript">
<!--
function divideFields() {
ltv.value = Aleft.value / Hval.value;
}
//-->
</script>

<input name="Hval" type="text" id="Hval">
<input name="Aleft" type="text" id="Aleft">
<input name="lvt" type="text" id="lvt">

dd/mm/yy
03-11-2004, 04:22 PM
Originally posted by Hrub
<script language="JavaScript" type="text/JavaScript">
<!--
function divideFields() {
ltv.value = Aleft.value / Hval.value;
}
//-->
</script>

<input name="Hval" type="text" id="Hval">
<input name="Aleft" type="text" id="Aleft">
<input name="lvt" type="text" id="lvt">

assuming you copied this directly from your page - the fields are spelt differently so that is you're reason.

also for clarity, you don't need both the name and id fields for an element, it would be clearer with just the one attribute used.

Hrub
03-11-2004, 04:43 PM
thanks,
fixed that and took out the name field.

I dont understand... I type in 100 in the first box, 2 in the second box and the third box still doesnt caculate the answer.

Please help me understand why
:confused:

dd/mm/yy
03-11-2004, 04:54 PM
this is how i got it working:



<script language="JavaScript" type="text/JavaScript">
<!--
function divideFields() {
num1 = Aleft.value
num2 = Hval.value

lvt.value = eval(num1 / num2)
}
//-->
</script>

<input name="Aleft"> / <input name="Hval">

<br><input name="lvt"><br><br>
<a href="javascript:divideFields()">Do Sum</a>


slightly more clear like that :thumbsup:

EDIT: For some reason the forum is showing a space between java and script in the href attribute (???) not exactly sure why (i didnt put it there, and its not showing up in the edit field) but it should be spelt as one word i.e: javascript

Hrub
03-11-2004, 05:20 PM
I noticed java and script were spaced and fixed that.
When I hit do sum, I get an error on page?

Is it me???

EDIT:
OK it does work. My text boxes are in a form.. which made it not work. How do i get that to work?

I guess it was me.

dd/mm/yy
03-11-2004, 11:05 PM
to get it to work with a form you need

document.FORMNAME.Aleft.value

where FORMNAME is the name of your form (i'm known for statin the obvious! :D )

Hrub
03-11-2004, 11:58 PM
Yeah!!!!! It works
You dont know how happy I am!!!!
:thumbsup:

Choopernickel
03-12-2004, 03:23 PM
Warning: offtopic.

Originally posted by dd/mm/yy
also for clarity, you don't need both the name and id fields for an element, it would be clearer with just the one attribute used.
Au contraire, daymonthyear. Each is required for different purposes. Form elements without a name attribute are not submitted in the HTTP request; form elements without an id attribute are not usable with elements like label, and force one to use the form[formname].elements[elementname] reference, completely disabling document.getElementById(elementname) references.

Clarity is not endangered by utilizing two attributes which have different purposes.

dd/mm/yy
03-12-2004, 04:13 PM
i stand corrected.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum