homepage Welcome to WebmasterWorld Guest from 54.197.171.109
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
javascript math
15 + 15=1515?
Birdman




msg:1476616
 8:52 pm on May 3, 2002 (gmt 0)

Hello. I'm trying to add two variables(numbers), but instead of totaling the numbers, they go back to back.

function buy()
{
var mat=document.veilopts.illusion.value;
var length1=document.veilopts.tier1.value;
var length2=document.veilopts.tier2.value;
var length3=document.veilopts.tier3.value;
var trim=document.veilopts.edge.value;
var motif=document.veilopts.fill.value;
var calcveil1=length1 + length2 + length3
var sOrder = "https://www.paypal.com/cart/add=1&business=m00n106%40aol.com&item_name=Veil w/"+mat+"bridal illusion and "+trim+" edge. Tier 1 is "+length1+" long. Tier 2 is "+length2+" long. Tier 3 is "+length3+" long. Illusion motif selected:"+motif+"&item_number=&amount="+ calcveil1;
alert(sOrder);
window.open(sOrder,'cartwin','width=600,height=400,scrollbars,location,resizable,status');
}

 

jatar_k




msg:1476617
 8:55 pm on May 3, 2002 (gmt 0)

I am sure that someone has a better answer than this but I have had to multiply the var before to convince them that they are numeric values.

var calcveil1=(length1 * 1) + (length2 * 1) + (length3 *1)

as I said there is probably something better but this works well.

(edited by: jatar_k at 8:56 pm (utc) on May 3, 2002)

pcguru333




msg:1476618
 8:55 pm on May 3, 2002 (gmt 0)

Javascript thinks that you are putting two strings back to back. You need to define them as intengers

pcguru333




msg:1476619
 9:17 pm on May 3, 2002 (gmt 0)

Try using the Math object.

someVar = Math.TotalLength(Length1+Length2);

Birdman




msg:1476620
 9:22 pm on May 3, 2002 (gmt 0)

Thank you jatar_k and pcguru333! I'm good to go, at least for now.
Peace:)

pcguru333




msg:1476621
 9:22 pm on May 3, 2002 (gmt 0)

If you need to convert a number to a string use

Var LengthString = SomeNumberVar.toString();

evinrude




msg:1476622
 9:23 pm on May 3, 2002 (gmt 0)

You can also do this:

var length1 = parseFloat(document.veilopts.tier1.value);
var length2 = parseFloat(document.veilopts.tier2.value);
etc...

ggrot




msg:1476623
 10:27 pm on May 3, 2002 (gmt 0)

To convert a number to string in j/s, its easier to use somenum = somenum + '';

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved