tags:

views:

34

answers:

1

i am trying to use the substring method inside a Js code wich echoes some php variable but it does not work.

$('#tarif_public_total').val( <?php echo $_tarif_public; ?> * $('#tarif_public').val() )

u=$('#tarif_membre').val()* <?php echo $_tarif_membre; ?> + $('#tarif_public').val()* <?php echo $_tarif_public; ?>
uu=u.substring(0,6)

$('#total_total').val(uu)

what im doing wrong here?

A: 

After using parseInt() on all the instances of val() (like Gert G suggested) you have to do:

uu=("" + u).substring(0,6)

or:

uu=String(u).substring(0,6)

Since you can't use substring on numbers. You have to cast back to a string using ("" + var)

Live demo of ("" + var)

Peter Ajtai
`String(var)` is not the preferred way to cast to a string, `(""+var)` is better and faster.
Ryan Tenney
Thank you this is getting me on the right path.
tada
Thank you Peter!
tada
@tada - You're welcome.
Peter Ajtai
I am using this on a text input where the user can increment or decrement the text input value but how do you shift the coma? (im using decimal values)
tada
Peter Ajtai