vendredi 4 octobre 2019

If statement function Hiding form inputs based on element value, How do I make it shorter?

I have a large chunk of javascript that hides and shows inputs based on the element being selected, I want to reduce the number of else ifs I need to use in this case. Basically anyway I can shorten this code would be good.

    function routerChange(element) {
         if ((element.value == '****') || (element.value == '****')){
            $('.network_hide').show();
            $('.net_password_hide').show();
            $('.activation_hide').show();
            $('.telephone_hide').show();
            $('.ip_address_hide').show();
            $('.ppp_username_hide').hide();
            $('.ppp_password_hide').hide();
            $('.lan_ip_hide').hide();
            $('.subnet_mask_hide').hide();
        } else if (element.value == '2862N_DTG'){
            $('.network_hide').hide();
            $('.net_password_hide').hide();
            $('.activation_hide').hide();
            $('.telephone_hide').hide();
            $('.ip_address_hide').hide();
            $('.ppp_username_hide').show();
            $('.ppp_password_hide').show();
            $('.lan_ip_hide').show();
            $('.subnet_mask_hide').show();
             $('#PPP').show();
        } else if ((element.value == '****') || (element.value == '****') || (element.value == '****') || (element.value == '****')){
            $('.network_hide').show();
            $('.net_password_hide').show();
            $('.activation_hide').show();
            $('.telephone_hide').show();
            $('.ip_address_hide').hide();
             $('.ppp_username_hide').hide();
             $('.ppp_password_hide').hide();
             $('.lan_ip_hide').hide();
             $('.subnet_mask_hide').hide();
        } else if (element.value == '****'){
            $('.network_hide').hide();
            $('.net_password_hide').hide();
            $('.activation_hide').show();
            $('.telephone_hide').show();
            $('.ip_address_hide').hide();
             $('.ppp_username_hide').hide();
             $('.ppp_password_hide').hide();
             $('.lan_ip_hide').hide();
             $('.subnet_mask_hide').hide();
        } else if ((element.value == '****') || (element.value == '****') || (element.value == '****')){
            $('.network_hide').show();
            $('.net_password_hide').show();
            $('.activation_hide').hide();
            $('.telephone_hide').hide();
            $('.ip_address_hide').hide();
             $('.ppp_username_hide').hide();
             $('.ppp_password_hide').hide();
             $('.lan_ip_hide').hide();
             $('.subnet_mask_hide').hide();
        } else if (element.value == '****'){
            $('.network_hide').hide();
            $('.net_password_hide').hide();
            $('.activation_hide').hide();
            $('.telephone_hide').hide();
            $('.ip_address_hide').hide();
             $('.ppp_username_hide').hide();
             $('.ppp_password_hide').hide();
             $('.lan_ip_hide').hide();
             $('.subnet_mask_hide').hide();
        } else {
            $('.network_hide').show();
            $('.net_password_hide').show();
            $('.activation_hide').show();
            $('.telephone_hide').show();
            $('.ip_address_hide').show();
            $('.ppp_username_hide').hide();
            $('.ppp_password_hide').hide();
            $('.lan_ip_hide').hide();
            $('.subnet_mask_hide').hide();
        }
            }

Just to be clear this code works as intended, I just want to reduce its size.

Aucun commentaire:

Enregistrer un commentaire