Form field labels do not hide when inputs/selects are hidden

Viewed 361

I have created a Bootstrap form and I am using the class form-floating on the controls.

When I hide some of the controls, the labels remain and are scrunched together. This happens to some controls but not to others. The code looks exactly the same so I am a bit miffed as to what is happening.

One thing I did notice is that when I move the hidden fields above the field that triggers the hide, the labels are hidden properly. However, if I move them back under the field that triggers hiding, the labels again remain and are overlapped.

HTML:

<div class="form-floating mb-4">
    <select class="form-select" id="frmProjectInterconnect" required>
        <option value="" selected disabled></option>
        <option>Yes</option>
        <option>No</option>
    </select>
    <label for="frmProjectInterconnect">Project Interconnection Point</label>
</div>

<div class="form-floating mb-4">
    <input type="number" class="form-control" id="frmInterconnectionCapacity" required>
    <label for="frmInterconnectionCapacity">Interconnection Capacity (Mbps)</label>
</div>

<div class="form-floating mb-4">
    <select class="form-select" id="frmDedicatedOrShared" required>
        <option value="" selected disabled> </option>
        <option>Dedicated</option>
        <option>Shared</option>
    </select>
    <label for="frmDedicatedOrShared">Dedicated or Shared?</label>
</div>

<div class="form-floating mb-4">
    <input type="text" class="form-control" id="frmServiceProvider" required>
    <label for="frmServiceProvider">Service Provider</label>
</div>

Javascript:

$('#frmProjectInterconnect').change(function(){
    if($(this).val() == "Yes") {
        $("#frmInterconnectionCapacity").show();
        $("#frmInterconnectionCapacity").attr('required', '');
        $("#frmInterconnectionCapacity").attr('data-error', 'This field is required.');
        $("#frmDedicatedOrShared").show();
        $("#frmDedicatedOrShared").attr('required', '');
        $("#frmDedicatedOrShared").attr('data-error', 'This field is required.');
        $("#frmServiceProvider").show();
        $("#frmServiceProvider").attr('required', '');
        $("#frmServiceProvider").attr('data-error', 'This field is required.');
    } else {
        $("#frmInterconnectionCapacity").hide();
        $("#frmInterconnectionCapacity").removeAttr('required');
        $("#frmInterconnectionCapacity").removeAttr('data-error');
        $("#frmDedicatedOrShared").hide();
        $("#frmDedicatedOrShared").removeAttr('required');
        $("#frmDedicatedOrShared").removeAttr('data-error');
        $("#frmServiceProvider").hide();
        $("#frmServiceProvider").removeAttr('required');
        $("#frmServiceProvider").removeAttr('data-error');
    }
});

Before triggering the hide

After triggering the hide

1 Answers

Have a try to show/hide the parent wrapper

$('#frmProjectInterconnect').change(function(){
    if($(this).val() == "Yes") {
        $("#frmInterconnectionCapacity").parent().show();
        $("#frmInterconnectionCapacity").attr('required', '');
        $("#frmInterconnectionCapacity").attr('data-error', 'This field is required.');
        $("#frmDedicatedOrShared").parent().show();
        $("#frmDedicatedOrShared").attr('required', '');
        $("#frmDedicatedOrShared").attr('data-error', 'This field is required.');
        $("#frmServiceProvider").parent().show();
        $("#frmServiceProvider").attr('required', '');
        $("#frmServiceProvider").attr('data-error', 'This field is required.');
    } else {
        $("#frmInterconnectionCapacity").parent().hide();
        $("#frmInterconnectionCapacity").removeAttr('required');
        $("#frmInterconnectionCapacity").removeAttr('data-error');
        $("#frmDedicatedOrShared").parent().hide();
        $("#frmDedicatedOrShared").removeAttr('required');
        $("#frmDedicatedOrShared").removeAttr('data-error');
        $("#frmServiceProvider").parent().hide();
        $("#frmServiceProvider").removeAttr('required');
        $("#frmServiceProvider").removeAttr('data-error');
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"></link>
<div class="form-floating mb-4">
    <select class="form-select" id="frmProjectInterconnect" required>
        <option value="" selected disabled></option>
        <option>Yes</option>
        <option>No</option>
    </select>
    <label for="frmProjectInterconnect">Project Interconnection Point</label>
</div>

<div class="form-floating mb-4">
    <input type="number" class="form-control" id="frmInterconnectionCapacity" required>
    <label for="frmInterconnectionCapacity">Interconnection Capacity (Mbps)</label>
</div>

<div class="form-floating mb-4">
    <select class="form-select" id="frmDedicatedOrShared" required>
        <option value="" selected disabled> </option>
        <option>Dedicated</option>
        <option>Shared</option>
    </select>
    <label for="frmDedicatedOrShared">Dedicated or Shared?</label>
</div>

<div class="form-floating mb-4">
    <input type="text" class="form-control" id="frmServiceProvider" required>
    <label for="frmServiceProvider">Service Provider</label>
</div>

Edit: For the case drop down option is at the end, and if we do not hide the parent wrapper, it looks like the labels are hidden, but in fact they are just behind the drop down. I add a 1 second timeout to hide the drop down, and the labels will be visible. See the code snippet.

$('#frmProjectInterconnect').change(function(){
        if($(this).val() == "Yes") {
            $("#frmInterconnectionCapacity").show();
            $("#frmInterconnectionCapacity").attr('required', '');
            $("#frmInterconnectionCapacity").attr('data-error', 'This field is required.');
            $("#frmDedicatedOrShared").show();
            $("#frmDedicatedOrShared").attr('required', '');
            $("#frmDedicatedOrShared").attr('data-error', 'This field is required.');
            $("#frmServiceProvider").show();
            $("#frmServiceProvider").attr('required', '');
            $("#frmServiceProvider").attr('data-error', 'This field is required.');
        } else {
            $("#frmInterconnectionCapacity").hide();
            $("#frmInterconnectionCapacity").removeAttr('required');
            $("#frmInterconnectionCapacity").removeAttr('data-error');
            $("#frmDedicatedOrShared").hide();
            $("#frmDedicatedOrShared").removeAttr('required');
            $("#frmDedicatedOrShared").removeAttr('data-error');
            $("#frmServiceProvider").hide();
            $("#frmServiceProvider").removeAttr('required');
            $("#frmServiceProvider").removeAttr('data-error');
            
            setTimeout(() => {
              $('#frmProjectInterconnect').hide();
            }, 1000);
        }
    });
   <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"></link>

    <div class="form-floating mb-4">
        <input type="number" class="form-control" id="frmInterconnectionCapacity" required>
        <label for="frmInterconnectionCapacity">Interconnection Capacity (Mbps)</label>
    </div>

    <div class="form-floating mb-4">
        <select class="form-select" id="frmDedicatedOrShared" required>
            <option value="" selected disabled> </option>
            <option>Dedicated</option>
            <option>Shared</option>
        </select>
        <label for="frmDedicatedOrShared">Dedicated or Shared?</label>
    </div>

    <div class="form-floating mb-4">
        <input type="text" class="form-control" id="frmServiceProvider" required>
        <label for="frmServiceProvider">Service Provider</label>
    </div>
    <div class="form-floating mb-4">
        <select class="form-select" id="frmProjectInterconnect" required>
            <option value="" selected disabled></option>
            <option>Yes</option>
            <option>No</option>
        </select>
        <label for="frmProjectInterconnect">Project Interconnection Point</label>
    </div>

Related