Javascript if/else with onClick event ?

  • Profile Image
    biiainsurance
    Asked on February 18, 2014 at 10:41 PM

     

    Not sure if my initial question made sense :o)

     

    @Elton wrote me a script for calculating rates but now I need to make two sets of rates based on a radio button trigger earlier in the form.

     

    The variable I am trying to use to check for value is set 

     

    var unitPurpose = document.getElementById('q67_unitPurpose'); 

     

    Then I check if that value is Remodeler to show one set of rates and if not show another set. The rates are set in a function so maybe the if /else needs to be outside of the function ?

     

    Form: 40187615170148 

     

    function getRate(){

    if (unitPurpose.value == 'Remodeler'){

    //first

    if (propert[0].checked == true && firstprocoast[0].checked == true) ratevalue.value = 0.55;

    if (propert[0].checked == true && firstprocoast[1].checked == true) ratevalue.value = 0.55;

    if (propert[0].checked == true && firstprocoast[2].checked == true) ratevalue.value = 0.55;

    } else {

    if (propert[0].checked == true && firstprocoast[0].checked == true) ratevalue.value = 0.14;

    if (propert[0].checked == true && firstprocoast[1].checked == true) ratevalue.value = 0.24;

    if (propert[0].checked == true && firstprocoast[2].checked == true) ratevalue.value = 0.12;

    }

    };

    #### would it work like above or would it work like below ? Either way I am still missing something but not sure where, was thinking an onClick event maybe ? 

     

    if (unitPurpose.value == 'Remodeler'){

    function getRate(){

    //first

    if (propert[0].checked == true && firstprocoast[0].checked == true) ratevalue.value = 0.55;

    if (propert[0].checked == true && firstprocoast[1].checked == true) ratevalue.value = 0.55;

    if (propert[0].checked == true && firstprocoast[2].checked == true) ratevalue.value = 0.55;

    };

    } else {

    function getRate(){

    if (propert[0].checked == true && firstprocoast[0].checked == true) ratevalue.value = 0.14;

    if (propert[0].checked == true && firstprocoast[1].checked == true) ratevalue.value = 0.24;

    if (propert[0].checked == true && firstprocoast[2].checked == true) ratevalue.value = 0.12;

    };

    };

  • Profile Image
    biiainsurance
    Answered on February 19, 2014 at 08:23 AM

    I even tried to if/else the click array

    unitPurpose.onclick = unitPurpose.onchange = function(){

    if (unitPurpose.value = 'Remodeler')

    {

    //click events

    for (var i = 0; i<firstprocoast.length; i++) {

      firstprocoast[i].onclick = 

      secondprocoast[i].onclick = 

      thirdprocoast[i].onclick = 

      fourthprocoast[i].onclick = getRateRemod;

    };

    } else {

    //click events

    for (var i = 0; i<firstprocoast.length; i++) {

      firstprocoast[i].onclick = 

      secondprocoast[i].onclick = 

      thirdprocoast[i].onclick = 

      fourthprocoast[i].onclick = getRate;

    };

    }

    }