views:

42

answers:

2

I have two fields that need to multiply each other and fill a 3rd form's value. Here's the HTML:

    <input type="text" name="estimate[concrete][price]" value="" onBlur="calc_concreteprice(document.forms.mainform);" /> 
per SF <strong>times</strong> 
<input type="text" name="estimate[concrete][sqft]" value="" onBlur="calc_concreteprice(document.forms.mainform);" /> SF
 = <input type="text" name="estimate[concrete][quick_total]" value="" />

Here's my JavaScript:

    function calc_concreteprice(mainform) {
        var oprice;
        var ototal;

        oprice = ((mainform.estimate[concrete][sqft].value) * (mainform.estimate[concrete][price].value));
        ototal = (oprice);

        mainform.estimate[concrete][quick_total].value = ototal;
    }

I want the first two forms to be multiplied together and output to the third. I think my problem may be within how I am referencing the input field names, with brackets (I'm taking results from this form as an array so I'm already used to working with the results as a multi-dimensional array).

Thanks for the help!

A: 

I'm pretty sure it's the square brackets. Try changing your names on your input fields. If you want definite separation between the names to show some sort of hierarchy, try using underscores like <input type="text" name="estimate_concrete_price" />.

In your javascript change it to mainform.estimate_concrete_price.value.....

Give that a shot. I'm pretty sure it will work.

Brent Parker
That would work, but I cannot change the name of the inputs. It has to remain estimate[concrete] with the brackets.
dmanexe
+3  A: 

When you write things like mainform.estimate[concrete][quick_total].value, it's attempting to access properties called concrete and quick_total. Try using this format instead to distinguish between a property and a string containing square brackets:

mainform['estimate[concrete][quick_total]'].value

Jimmy Cuadra
I appreciate the practice, it's a good decision to cite references like that, but it doesn't work.
dmanexe
What happens when you try it? Do you get an error or anything? I just tried it and it works for me.
Jimmy Cuadra
Works for me too. Are you using firebug to debug? If not you should check it out.
ghostz00