lundi 17 février 2020

Switch chekcbox SHOW / HIDE two DIV elements using Vanilla Javascript

I have two Form Groups, both are inside a DIV element. Fist one have an id="personaFisicaFormGroup"

Second one have an id="personaMoralFormGroup"

personaFisicaFormGroup is display: block; as default

personaMoralFormGroup is display: none; as default

When user click the checkbox I want to inverse the display.

personaFisicaFormGroup set to display: none;

personaMoralFormGroup set to display: blcok;

If user click again I want to switch values block and none (indeterminate times).

Here is my code:

const onOffSwitch = document.getElementById('onOffSwitch');
const personaFisicaFormGroup = document.getElementById('personaFisicaFormGroup');
const personaMoralFormGroup = document.getElementById('personaMoralFormGroup');


onOffSwitch.addEventListener('change', e => {
    if(e.target.checked){
        personaMoralFormGroup.style.display = 'block';
    } else {
        personaFisicaFormGroup.style.display = 'none';
    }
});
.nuevo-ingreso-form__group {
    border: 1px solid var(--clr-grey-borders);
    border-radius: 5px;
    margin-bottom: 20px;
    background-color: rgb(255, 255, 255);
    width: 300px;
}
#personaMoralFormGroup {
    display: none;
}
.nuevo-ingreso-form__group:hover {
    box-shadow: 0 7px 16px 0 rgba(0,0,0,.2), 0 1px 3px 0 rgba(0,0,0,.1);
    transition: box-shadow .3s ease-out,-webkit-box-shadow .3s ease-out;
}

.nuevo-ingreso-form__group-header {
    padding: 25px 40px 10px;
    display: flex;
    align-items: center;
    font-size: 18px;
}
.nuevo-ingreso-form__group-header h3 {
    margin-left: 20px;
}
.nuevo-ingreso-form__field-wrapper {
    padding: 0px 40px;
    padding-top: 14px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.nuevo-ingreso-form__field-wrapper:last-child {
    padding-bottom: 50px;
    
}
.nuevo-ingreso-form__input-label {
    font-size: 13px;
    font-weight: 800;
    color: grey;
    padding: 5px 5px 5px 0;
    display: block;
    width: 40%;
    text-align: right;
}
.nuevo-ingreso-form__input-container {
    border: 1px solid grey;
    height: 100%;
    border-radius: 5px;
    flex-grow: 1;
    margin-left: 20px;
}
.nuevo-ingreso-form__input-field {
    margin: 0;
    background-color: transparent;
    border: none;
    width: 100%;
    font-size: 16px;
}

.onoffswitch {
    position: relative; width: 172px;
    -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
    margin: 0 auto;
}
.onoffswitch-checkbox {
    display: none;
}
.onoffswitch-label {
    display: block; overflow: hidden; cursor: pointer;
    border: 2px solid #FFFFFF; border-radius: 50px;
}
.onoffswitch-inner {
    display: block; width: 200%; margin-left: -100%;
    transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
    display: block; float: left; width: 50%; height: 35px; padding: 0; line-height: 35px;
    font-size: 15px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
    box-sizing: border-box;
}
.onoffswitch-inner:before {
    content: "Persona Física";
    padding-left: 19px;
    background-color: #E0E0E0; color: #5C5C5C;
}
.onoffswitch-inner:after {
    content: "Persona Moral";
    padding-right: 19px;
    background-color: #636363; color: #FFFFFF;
    text-align: right;
}
.onoffswitch-switch {
    display: block; width: 18px; margin: 8.5px;
    background: #FFFFFF;
    position: absolute; top: 0; bottom: 0;
    right: 133px;
    border: 2px solid #FFFFFF; border-radius: 50px;
    transition: all 0.3s ease-in 0s; 
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
    margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
    right: 0px; 
}
<div class="onoffswitch">
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="onOffSwitch">
    <label class="onoffswitch-label" for="onOffSwitch">
        <span class="onoffswitch-inner"></span>
        <span class="onoffswitch-switch"></span>
    </label>
</div>

<!-- DATOS GENERALES - PERSONA FISICA -->
<div id="personaFisicaFormGroup" class="nuevo-ingreso-form__group">
    <div class="nuevo-ingreso-form__group-header">
        <h3>Persona Fisica</h3>
    </div>
    <div class="nuevo-ingreso-form__field-wrapper">
        <label class="nuevo-ingreso-form__input-label" for="">NOMBRE</label>
        <div class="nuevo-ingreso-form__input-container">
            <input id="clienteNombreORazonSocial" class="nuevo-ingreso-form__input-field" type="text">
        </div>
    </div>
    <div class="nuevo-ingreso-form__field-wrapper">
        <label class="nuevo-ingreso-form__input-label" for="">APELLIDO PATERNO</label>
        <div class="nuevo-ingreso-form__input-container">
            <input id="clientePaterno" class="nuevo-ingreso-form__input-field" type="text">
        </div>
    </div>
    <div class="nuevo-ingreso-form__field-wrapper">
        <label class="nuevo-ingreso-form__input-label" for="">APELLIDO MATERNO</label>
        <div class="nuevo-ingreso-form__input-container">
            <input id="clienteMaterno" class="nuevo-ingreso-form__input-field" type="text">
        </div>
    </div>
</div>

 <!-- PERSONA MORAL -->
<div id="personaMoralFormGroup" class="nuevo-ingreso-form__group">
    <div class="nuevo-ingreso-form__group-header">
        <i class="fa fa-id-card"></i>
        <h3>Persona Moral</h3>
    </div>
    <div class="nuevo-ingreso-form__field-wrapper">
        <label class="nuevo-ingreso-form__input-label" for="">RAZÓN SOCIAL</label>
        <div class="nuevo-ingreso-form__input-container">
            <input id="clienteNombreORazonSocial" class="nuevo-ingreso-form__input-field" type="text">
        </div>
    </div>
    <div class="nuevo-ingreso-form__field-wrapper">
        <label class="nuevo-ingreso-form__input-label" for="">RFC</label>
        <div class="nuevo-ingreso-form__input-container">
            <input id="clienteRFC" class="nuevo-ingreso-form__input-field" type="text">
        </div>
    </div>
    <div class="nuevo-ingreso-form__field-wrapper">
        <label class="nuevo-ingreso-form__input-label" for="">EMAIL</label>
        <div class="nuevo-ingreso-form__input-container">
            <input id="clienteEmail" class="nuevo-ingreso-form__input-field" type="text">
        </div>
    </div>
</div>



Aucun commentaire:

Enregistrer un commentaire