#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    44
    Rep Power
    14

    Angry Javascript Addition


    Help

    I am trying to add two numbers from text box in a window in a iframe. The only problem is they are strings so when i use the + sign they build the string. This is the code

    value1=window.parent.SelectSquad.goalkeeper1_value.value;
    value2=window.parent.SelectSquad.goalkeeper2_value.value;

    total=value1 + value2;
    window.parent.SelectSquad.spent.value=total;

    Does anyone know how to get them to read as numbers

    Thanks
  2. #2
  3. yet another member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    262
    Rep Power
    14
    Hi!

    You might look at parseInt:

    total=parseInt(value1) + parseInt(value2);

    There also is a parseFloat function, respectively.


    Greetings,

    Atrus.
  4. #3
  5. No Profile Picture
    Senior Citizen
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2001
    Location
    leftcoast
    Posts
    2,019
    Rep Power
    16
    You could also try:

    function dispSpent() {
    var currForm = window.parent.SelectSquad;
    value1 = currForm.goalkeeper1_value.value;
    value2 = currForm.goalkeeper2_value.value;
    if (isNaN(value1) || parseInt(value1) != value1) {
    alert('Please enter an integer (whole number).');currForm.goalkeeper1_value.focus();currForm.goalkeeper1_value.select();return false;
    }
    if (isNaN(value2) || parseInt(value2) != value2) {
    alert('Please enter an integer (whole number).');currForm.goalkeeper2_value.focus();currForm.goalkeeper2_value.select();return false;
    }
    total = eval(value1) + eval(value2);
    currForm.spent.value = total;
    }

    [included a little error checking]
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2001
    Posts
    44
    Rep Power
    14

    Thumbs up Thanks


    Thanks guy, worked a treat

IMN logo majestic logo threadwatch logo seochat tools logo