views:

110

answers:

3

I have this code in jquery :

    $("#order_btn").click(function(){
        var totalprice = $(".price_amount").text();
        $("#totalprice").val(totalprice);
    });

When I submit this form with a hidden value i will get the totalprice value two times, if its 200000 i will get 200000200000. why ?

<input type="hidden" value="" name="totalprice" id="totalprice">
<input id="order_btn" type="submit" class="submit" name="submit" value="submit">

the price amout will defined here :

<span class="price_amount">75000</span>

I have this span tag two times, but I need both of them, is there a way to get one value only ?

A: 

Are you sure there's only one element on your page with the price_amount class? If you put a breakpoint (or an alert) on the value of totalprice after it's been assigned what do you get?

One other thing - and I'm not sure if this matters or not, but I would put this code in a handler for your form submit instead of click.

Andy Gaskell
Ok i will try that :)
Datis
A: 

Because you have two <span> elements with the class "price_amount".

It's this line in particular

var totalprice = $(".price_amount").text();

When you call .text() from a jQuery set, it will aggregate that value for all selected nodes and return it.

So, either make sure you have only one span w/that classname, or make your selector more specific/granular so that you only retrieve the desired node.

Peter Bailey
Thats right I have two element of this class :) thnx
Datis
A: 
gabtub
I have tried it but id didnt give me any value !
Datis
that's strange, you should be able to get the values of you "price_amount" span tags like thattry using $(".price_amount").get()[0].text()
gabtub
it should be `$(".price_amount:first").text()`
SilentGhost
@silentghost +1 sry, mixed that up...
gabtub