Ways to display multiple select options based on the user's selection of multiple choices and ensure they remain hidden and reset when deselected with the use of

There is a select options field that contains languages, where the user can choose multiple language options or just one. When the user selects a language, a new select option should appear for them to choose their proficiency level in that language.

If the user selects multiple languages (e.g. English, Arabic, French), then three select options will appear for each language proficiency level. If the user unselects any of the language options, the corresponding select option for the proficiency level should hide and reset to the default selection.

However, there are some issues with my code:

  1. When the user selects multiple options, not all select options for proficiency levels appear at once.
  2. When the user unselects an option, it remains visible and does not reset to the default selection.

Could someone please assist me in resolving these problems?

Below is my view blade with script:

@extends('layouts.app')
@section('content')

<form>
    <div class="form-row">
        <div class="form-group col-md-6">
            <label for="languages">Languages</label>
            <select id="languages" class="form-control" multiple>
                <option>Choose Language...</option>
                <option value="1">English</option>
                <option value="2">Arabic</option>
                <option value="3">France</option>
                <option value="4">Spanish</option>
            </select>
        </div>
    </div>

    <!-- Additional select options for language proficiency levels -->
    <div id="arabicShow" style="display: none" class="form-row">
        <div class="form-group col-md-6">
            <label for="arabic">Arabic level</label>
            <select id="arabic" class="form-control">
                <option value="" selected>Choose Your level...</option>
                <option value="">a</option>
                <option value="">b</option>
            </select>
        </div>
    </div>

    <div id="englishShow" style="display: none" class="form-row">
        <div class="form-group col-md-6">
            <label for="english">English level</label>
            <select class="form-control">
                <option value="">Choose Your level...</option>
                <option value="">a</option>
                <option value="">b</option>
            </select>
        </div>
    </div>

    <div id="franceShow" style="display: none" class="form-row">
        <div class="form-group col-md-6">
            <label for="france">French level</label>
            <select class="form-control">
                <option value="">Choose Your level...</option>
                <option value="">a</option>
                <option value="">b</option>
            </select>
        </div>
    </div>

    <button type="submit" class="btn btn-primary">Save</button>
</form>

@stop
@section('script')
<script type="text/javascript">

$(document).ready(function (){
    $("#languages option:first-child").attr("disabled","disabled");
    $("#arabic option:first-child").attr("disabled","disabled");
    $("#english option:first-child").attr("disabled","disabled");
    $("#france option:first-child").attr("disabled","disabled");

    $('#languages').change(function(){
        var text = $(this).find("option:selected").text().trim(); //get text
        if(text === "Arabic"){
            $('#arabicShow').show();

        }else if(text === "English"){
            $('#englishShow').show();
        }else if(text === "France"){
        $('#franceShow').show();
    }else {
            $('#arabicShow').hide();
            $('#englishShow').hide();//hide
        }
    });
});
</script>

@stop

Answer №1

To cycle through all chosen options and display necessary select-boxes based on conditions, you can utilize the .each loop.

Example Code :

$(document).ready(function() {
  $("#colors option:first-child").attr("disabled", "disabled");
  $("#red option:first-child").attr("disabled", "disabled");
  $("#green option:first-child").attr("disabled", "disabled");
  $("#blue option:first-child").attr("disabled", "disabled");
  $('.options').hide(); //hide all
  $('#colors').change(function() {
    $('.options select:not(:visible)').val(""); //reset 
    $('.options').hide(); //hide all
    //iterate through each selected option..
    $(this).find("option:selected").each(function() {
      var choice = $(this).text()
      if (choice === "Red") {
        $('#redDisplay').show();
      } else if (choice === "Green") {
        $('#greenDisplay').show();
      } else if (choice === "Blue") {
        $('#blueDisplay').show();
      }
    })

  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<form>
  <div class="form-row">
    <div class="form-group col-md-6">
      <label for="colors">Colors</label>
      <select id="colors" class="form-control" multiple>
        <option>Choose Color...</option>
        <option value="1">Red</option>
        <option value="2">Green</option>
        <option value="3">Blue</option>
      </select>
    </div>
  </div>
  <!--added a common class : options-->
  <div id="redDisplay" class="form-row options">
    <div id="" class="form-group col-md-6">
      <label for="red">Shade of Red</label>
      <select id="red" class="form-control">
        <option value='' selected>Select Shade...</option>
        <option value=''>Light</option>
        <option value=''>Dark</option>
      </select>
    </div>
  </div>

  <div id="greenDisplay" class="form-row options">
    <div class="form-group col-md-6">
      <label for="green">Tone of Green</label>
      <select class="form-control">
        <option value=''>Select Tone...</option>
        <option value=''>Pale</option>
        <option value=''>Deep</option>
      </select>
    </div>
  </div>
  <div id="blueDisplay" class="form-row options">
    <div class="form-group col-md-6">
      <label for="blue">Hue of Blue</label>
      <select class="form-control">
        <option value=''>Select Hue...</option>
        <option value=''>Aqua</option>
        <option value=''>Navy</option>
      </select>
    </div>
  </div>


  <button type="submit" class="btn btn-primary">Save</button>
</form>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for obtaining Array elements within a JSON object utilizing handlebars

var express = require('express'); var router = express.Router(); var data = { products :{ name:'Computers', price:'500' }, items:[ {name: 'Keyboard' , cost:'50'}, {name: 'M ...

Differences in vertical font positioning between Mac and Windows systems

One element on my page is a quantity balloon for a basket, but I'm struggling to get it looking right. The font and vertical position don't seem to be cooperating for such a simple element: https://i.sstatic.net/IfSi6.png Despite trying solutio ...

How to Build a Number Spinner Using Angular Material?

Is there a number spinner in Angular Material? I attempted to use the code provided in this question's demo: <mat-form-field> <input type="number" class="form-control" matInput name="valu ...

Creating a dynamic menu system using ASP.NET, JQuery, and Suckerfish for optimum functionality

I want to create a dynamic menu from a table using the Suckerfish CSS menu and JQuery. I'm following this example as my guide: Suckerfish menu with ASP.NET and JQuery. Currently, I have managed to make it work by manually adding links, similar to what ...

Is it possible to incorporate swigjs within scripts?

Currently, I am stuck while working on my website using a combination of nodejs, express, and swigjs. The issue I am facing involves a <select> element that is populated by options from a variable passed to my template. When a user selects an option, ...

The issue of Google Maps API Marker Images not appearing in Internet Explorer

I am working with the Google Maps API Javascript V3 and encountering an issue specifically in Internet Explorer where my marker images are not displaying. Strangely, Adobe Browserlab shows that other browsers do not have this problem. The coordinates are ...

What is the best way to ensure my arrow text remains properly positioned when using fullpage.js?

Seeking guidance from the web development community. I am currently working on a client's website that utilizes fullpage.js and encountering a persistent issue. On slide1 of the website, I am struggling to display the correct text next to the arrows. ...

Easily retrieving row values from a Repeater with jQuery

A challenge I'm facing involves a jQuery function that triggers on the onchange event of a textbox. The goal is to validate the user input in the textbox against the values in a datatable displayed by a repeater. While I managed to retrieve the textbo ...

Managing user logins across different sessions using passport.js, mysql database, and express-session

Currently, my app utilizes Passport.js for user authentication with Facebook, which is functioning properly. The issue arises when my node.js server is restarted and the users are automatically logged out. It appears that using express-sessions would be a ...

CSS: Adjusting color when hovering

I've been working on a website featuring clickable images, but I'm struggling with the ...:hover function. My goal is to have the image overlayed with a white color at 0.1 opacity upon hovering. <html> <head> <style> ...

What is causing the Bootstrap 5 navbar to not collapse?

Struggling to compile Bootstrap 5 with sass, I have a feeling that I might be missing some important scss files. When I added the <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7 ...

Fixing the Load More Posts Ajax Button in Wordpress

While browsing through this page authored by @dingo_d, I stumbled upon a great method for loading more posts with ajax. However, I seem to be facing an issue that I cannot solve on my own. I would greatly appreciate it if the author or someone else could a ...

Tips for saving/downloading generated QR codes in React Native

Using this code allows me to generate QR Codes, but I am struggling with saving the generated QR Code in PNG or JPEG format. I have tried a few examples without success and I am continuing to try different methods. import React, { Component } from 'r ...

Querying data from a label using jQuery

I am facing a challenge with reading label attributes in jQuery. I have multiple labels, each with the same class. These labels contain important information that I need to access. However, despite being able to select the object using $, I am struggling ...

Tips for initiating a component within a loop and terminating it after completing all 3 iterations

Looking for a solution to open and close tags in a loop every 3 iterations. The objective is to create a grid using container, row, and column elements. However, I am unsure how to achieve this. Example: render(){ const arrayName = ["john", " ...

How to truncate text in a cell of a Vuetify data table using CSS styling

Is there a way to truncate text in a cell using CSS without it wrapping around or overflowing? The ideal CSS code should include: .truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } However, the current implementation caus ...

Ensure that the application logs out automatically after the browser is closed

I'm working on an ASP.Net Web application and I need to implement a feature where the user is automatically logged out when they close their browser, regardless of the browser type (Google Chrome, IE, Firefox). Any suggestions on how to achieve this? ...

Implementing event handlers with 'v-on' on dynamically inserted content using 'v-html' in Vue

Can Vue allow for the addition of v-on events on strings included in v-html? In this scenario, clicking the 'Maggie' link doesn't produce any action. It appears that it's not recognized by Vue. Is there an alternative method to achieve ...

Exploring the implementation of window.addEventListener within an Angular project

I am currently working on testing a method in Angular using Jasmine. However, I am running into an issue with triggering the mouse event (specifically when the browser back button is clicked). Below is the code snippet I'm working with: navigate() { ...

Mouse over effect to highlight the crosshair on a table

My code currently enables a crosshair function, but I am looking for a way to limit the highlight effect only within the cursor (hover) area. Instead of highlighting in a "cross" shape, I would like it to show a backward "L" shape. This means that the hig ...