display a different selection option depending on the previously chosen item

I'm working on displaying additional options in a select box when the user makes a selection from the main select box. I've set display:none for the select boxes, which will only appear when the user chooses an option from the main select box.

Could someone assist me with either jQuery or JavaScript specifically for the first choice?

.sub{display:none;}

<select><!--This is the main select box-->
<option value="">Select</option>
<option>ONE</option>
<option>two</option>
<option>three</option>
<option>four</option>
<option>five</option>
</select>

<select class="sub"><!--Another select box for option one-->
<option>test1</option>
<option>test1</option>
</select>

<select class="sub"><!--Another select box for option two-->
<option>test2</option>
<option>test2</option>
</select>

<select class="sub"><!--Another select box for option three-->
<option>test3</option>
<option>test3</option>
</select>

<select class="sub"><!--Another select box for option four-->
<option>test4</option>
<option>test4</option>
</select>

<select class="sub"><!--Another select box for option five-->
<option>test5</option>
<option>test5</option>
</select>

Answer №1

Utilizing the selectedIndex property of DOM Select Element, you can determine the index of the selected Option. By employing jQuery's .eq() method and monitoring the change event, it is possible to choose the desired select element from jQuery's index-based collection:

var $sub = $('select.sub');

$('select').first().change(function() {    
    $sub.hide();
    // Check if the first option is not selected
    if (this.selectedIndex > 0)
       $sub.eq(this.selectedIndex - 1).show();
});

http://jsfiddle.net/7CmYj/

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

Angular JS: Extracting the header from a CSV file

Just getting started with angular JS and I have a question. I need to take a CSV file from the user as input and then send it to the controller when they click submit. <button class="btn btn-primary" type="submit" ng-click="Input(File)">Submit</ ...

Aligning buttons in an inline form with Bootstrap

Issue with Button Alignment Post Validation. <form class="form-inline form-padding"> <div class="form-group"> <div class="input-group"> <input class="form-control" placeholder="First Name" name="fir ...

Create a global variable by importing an external module in TypeScript

I am currently developing a TypeScript npm module called https://www.npmjs.com/package/html2commonmark. This module is versatile and can be utilized in nodejs (via require) as well as in the browser (by loading node_modules/html2commonmark/dist/client/bund ...

"Error message pops up indicating the dispatcher is missing while using npm link with a local project

Currently, I am working on a React component library that I want to integrate into a local project for testing purposes. My initial approach was to use npm link to connect the component library with my local project. However, during this process, I encount ...

What is the best way to transfer information to a different NextJS page?

I want to implement a search input field. The idea is to allow the user to type something into the search bar and then send that input to another page where an API request will be made with the search content. Essentially, when the user types "something" ...

Retrieving components from Ajax response data

While I have a good grasp of PHP, diving into AJAX and dealing with JSON is proving to be quite challenging for me. My PHP script simply delivers a straightforward JSON string like this: {"bindings": [ {"ircEvent": "PRIVMSG", "method": "newURI", "regex": ...

Master the art of directing your attention to a list element with ease using the tab function and jQuery

I've been tasked with creating a questionnaire for a client. They want the current active question to be displayed at 100% opacity, while the inactive questions should be at 20% opacity. Currently, when the page loads, all questions are dimmed to 20% ...

Issue with event.stopPropagation() in Angular 6 directive when using a template-driven form that already takes event.data

I am currently developing a citizenNumber component for use in forms. This component implements ControlValueAccessor to work with ngModel. export class CitizenNumberComponent implements ControlValueAccessor { private _value: string; @Input() place ...

Proper method for verifying line-clamp in a Vue component

I came across a question that aligns perfectly with my current task - determining if text is truncated or not. The query can be found here: How can I check whether line-clamp is enabled?. It seems like my approach may not be accurate, so any guidance on ho ...

Errors occurring when trying to parse JSON in Firefox with Firebug, but strangely only happening

Encountering a puzzling error with our application that handles JSON calls. While the app functions flawlessly on various environments and browsers, Firefox presents an issue specifically when run locally. Upon running the code locally, numerous errors occ ...

Monitor the traffic on my website by implementing .htaccess restrictions

I am maintaining a website with restricted access to specific IP addresses listed in the .htaccess file. I am looking to implement a logging system that tracks unauthorized attempts to access the site. Is it feasible to record each attempt in a database? ...

Unchecking an available item observable in Knockout.js when clicking on a selected item

When you click on the elements in the top list, a selection is made. If you click on the elements in the bottom list, it will be removed from the list. However, the checkbox in the top list is not being unchecked. How can this issue be resolved? functio ...

Having trouble getting the vue-slick-carousel to function properly when using it from the CDN

Struggling to implement a small app using the CDN script at , but so far no success. I'm a novice with vue.js and unsure if I need to import anything. According to the documentation, importing is required: Vue-slick-carousel Following this structure ...

Can anyone tell me the method to retrieve the id of the current element that initiated the horizonSwiper event in JavaScript?

I successfully integrated horizonSwiper into my php(Yii2) website to display images from different albums in a horizontal row with the ability to scroll left and right. Now, I am looking to implement lazy loading when scrolling or swiping left/right. Howev ...

How can I display a popup window when a validation event occurs?

Having trouble finding a suitable solution for my problem. Our MVC 3 application includes a form with Data Annotations for validation. The Email field has a RemoteAttribute to check for uniqueness. Here's what needs to be implemented: If the Email ...

Axios successfully fulfills a promise despite the request being canceled while using React.StrictMode

In my React project, I'm using axios with React.StrictMode enabled by default, causing components to render twice. To handle this behavior, I've set up an abort control for my axios instance and call its abort() method in the useEffect clean-up f ...

Tips for creating a responsive ReactJS website with Material-UI

I've been working on a reactjs project with material-ui, and everything looks great on desktop. However, when I switch to the mobile layout, all the text and images start overlapping. I'm wondering how I can make my site responsive. Below you&apo ...

There appears to be an issue with the functionality of jQuery draggable

My webpage has the following HTML structure: <!DOCTYPE html> <html lang="en"> <head> <link type="text/css" rel="stylesheet" href="stylesheet.css"/> <script type="text/javascript" src="http://code.jquery.com/ ...

Employ CSS flexbox and/or JavaScript for creating a customizable webpage

I am in the process of developing a basic IDE for an educational programming language that has similarities to Karel the Dog. One major issue I am encountering is creating the foundation HTML page. The IDE consists of 4 main areas: Toolbox (contains but ...

Trouble arises with the chrome extension code for retrieving tweets from Twitter

I'm currently working on developing a Chrome extension that will display tweets featuring the hashtag perkytweets within the extension's popup. However, I'm facing an issue where nothing is being displayed. Let's take a look at the cod ...