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

    Join Date
    May 2002
    Posts
    126
    Rep Power
    12

    Question By passing Javascript code, please help


    I have a javascript that handles an array of input boxes. I need to wrap that script with an if statement that checks to see if the radio button assigned to it is checked. So if the radio button for the percentage column is checked it will allow the program into that script. If it is not checked it needs to by pass that script and move on. It keeps on jumping into the code, even if the button is not checked. Here is the code:

    <script type='text/javascript' language='javascript'>

    if (document.test.radiocommtype[0].checked == true){
    function filter(e, field) {
    var kC = (e && e.keyCode) ? e.keyCode :
    (e && e.which) ? e.which : null;
    if (!kC || kC == 8) return true;
    else return (kC>47 && kC<58);
    }

    function sumPctOnblur(field) {
    var el, e = 0, total_pct = 0, f = field.form;
    if (typeof f[field.name].length == 'undefined') {
    field.value = '100';
    f.TOTALPERCENT.value = '100';
    alert('Since you are a single agent on this deal, your split will be automatically converted to 100%.');
    return;
    } else {
    if (!field.value) return;
    field.value = parseInt(field.value);
    while (el = f[field.name][e++])
    if (/^\d{1,3}/.test(el.value)) total_pct += parseInt(el.value);
    f.TOTALPERCENT.value = total_pct;
    }
    }

    function sumPctOnsubmit(grp) {
    var el, e = 0, total_pct = 0;
    if (typeof grp.length == 'undefined') {
    grp.value = '100';
    grp.form.TOTALPERCENT.value = '100';
    return;
    } else {
    while (el = grp[e++])
    if (/^\d{1,3}/.test(el.value)) total_pct += parseInt(el.value);
    else {alert('Please enter a valid percentage (n%) for all agents.');
    el.focus();
    el.select();
    return false;
    }
    if (total_pct != 100) {
    var msg = 'The total of all agents percentages should be 100%, please adjust your percentages.';
    alert(msg);
    grp[0].focus();
    grp[0].select()
    ;//optional
    return false;
    }
    }
    return true;
    }

    }//end if statement
    </script>
    Scott Orton
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    147
    Rep Power
    11
    First, it really helps when you put the [ code ] tag around your code to preserve formatting.

    Second, it looks like you are trying to put an if statement around your function definitions. What you want to do instead is put the if statement around the part of your script that calls those funcitons. For instance, when your write in your script:
    Code:
    function my_func(){
         alert('hi');
    }
    the 'alert' doesn't happen until you call your function like this:
    Code:
    my_func();
    you can then add your control sequence around that call:
    Code:
    if(x == y) {
         my_func();
    } else {
         my_other_func();
    }
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    126
    Rep Power
    12

    Is this what you mean


    onBlur="if(document.test.commradiotype[0].checked == true){sumPctOnblur(this)}"
    Scott Orton
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    147
    Rep Power
    11
    that should work, though it's a little complex. you could try putting that if statement inside your function too.

IMN logo majestic logo threadwatch logo seochat tools logo