views:

346

answers:

3

I'm embarrassed by the 'basicness' of this question, but after wasted hours, here goes.

In an effort to do something with the jQuery Calculation plugin, I am playing with the basic example of the order form on the plugin site. I want to have the grand total as a form field,rather than text, so I can use the value.

The function that calculates and shows the grand total is:

    function ($this){
                // sum the total of the $("[id^=total_item]") selector
                var sum = $this.sum();

                $("#grandTotal").text(
                    // round the results to 2 digits
                    sum.toFixed(2)
                );
            }

the total updates on keyup in:

<span id="grandTotal"></span>

But this does not work with:

<input type="text" id="grandTotal" value=""/>

Can anyone point me to what I need to add/change to make that work? To call my javascript basic would be a compliment, so please talk to me like I know nothing!

+4  A: 

You can set the value of input fields with jQuery's .val() method:

$("#grandTotal").val(sum.toFixed(2));

Just to be complete: If you have a textarea, then you can set the text inside the textarea with the .text() method (I am not sure about .val() though, I think this really only works on the other form elements).

Felix Kling
Thanks, and thanks for not calling me an idiot :) Ditto to everyone else.
Katherine
@Katherine: What do you think *my* problems were at the beginning? There is always a start, good if someone else helps you. Btw you should accept this answer if it helped you.
Felix Kling
A: 

function ($this){ // sum the total of the $("[id^=total_item]") selector var sum = $this.sum();

            $("#grandTotal").val( 
                // round the results to 2 digits 
                sum.toFixed(2) 
            ); 
        } 

Try that.

change .text to .val

Levi Hackwith
A: 

You need to use val method to insert value into the textbox:

$("#grandTotal").val(sum.toFixed(2));
Sarfraz