automatically changing the input type based on the selected option in the dropdown menu

Currently, I am working on a project that involves attendance management. One of the key features I am implementing is a drop-down box where users can select a month. When a month is selected, a dynamic number of buttons will be generated in a separate division. For example, if January is chosen, 31 buttons will appear, and so on for other months.

Below is an excerpt from my HTML code:


                var counterButton = 0;
                
                function addAllInputs(divName, inputType) {
                    var newdiv = document.createElement('div');
                    
                    switch(inputType) {
                        case 'January':
                            for(i=0;i<31;i++)
                            {
                                newdiv.innerHTML = "Entry " + (counterText + 1) + "<br><input type='button' name='myInputs[]'>";
                                counterText++;
                            }               
                            break;
        
                        case 'February':
                            for(i=0;i<28;i++)
                            {
                                newdiv.innerHTML = "Entry " + (counterText + 1) + "<br><input type='button' name='myInputs[]'>";
                                counterText++;
                            }               
                            break;
        
                        case 'March':
                            for(i=0;i<31;i++)
                            {
                                newdiv.innerHTML = "Entry " + (counterText + 1) + "<br><input type='button' name='myInputs[]'>";
                                counterText++;
                            }               
                            break;
        
                        case 'April':
                            for(i=0;i<30;i++)
                            {
                                newdiv.innerHTML = "Entry " + (counterText + 1) + "<br><input type='button' name='myInputs[]'>";
                                counterText++;
                            }               
                            break;
                    }
                    
                    document.getElementById(divName).appendChild(newdiv);
                }
            

                <pre>
                    <div>
                        <select>
                            <option value="">January</option>
                            <option value="">February</option>
                            <option value="">March</option>
                            <option value="">April</option>
                            <option value="">May</option>
                            <option value="">June</option>
                            <option value="">July</option>
                            <option value="">September</option>
                            <option value="">October</option>
                            <option value="">November</option>
                            <option value="">December</option>
                        </select>
                    </div>
                </pre>
            

Answer №1

To capture the change event on the select element, you should set up an event listener and trigger your function when the event occurs. Here is an example of how you can achieve this:

var selectElement = document.querySelector('select');
selectElement.addEventListener("change", function() {
    customFunction();
});

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

Sorting multiple table rows simultaneously using Jquery UI

Having trouble with jQueryUI sorting. Currently, I have a table with sorting on tr and it's functioning properly. However, I now need to sort two rows at once. Specifically, I need to sort together rows with G1, G2, or G3. After struggling with this ...

Homepage featuring a stacked image background similar to Kickstarter

I am trying to replicate the stacked image effect seen on kickstarter.com. However, I am facing an issue where the images do not overflow on the screen as desired. The arrow indicates the area that needs to be filled with the image. Check out the image he ...

Using Jquery and AJAX to dynamically fill out an HTML form based on dropdown selection

My goal is to populate a form on my webpage with information pulled from a MySQL database using the ID of the drop-down option as the criteria in the SQL statement. The approach I have considered involves storing this information in $_SESSION['formBoo ...

The 'props.p' navigation in react-native is undefined

I have gone through various forums and discussions regarding this issue, but none of the solutions seem to work for me. For some reason, I am facing difficulties passing props to react-navigation when I attempt to navigate, resulting in the following erro ...

Submitting information to a server

I have a simple Angular 6 app with follow and unfollow buttons. When you click follow, the number increases. I want to save these follower numbers to a JSON server. Here is the link to the JSON server documentation: JSON Server Documentation To see a dem ...

Issue with background image responsiveness in Bootstrap jumbotron class when resizing the screen

Currently, I am delving into the world of bootstrap as I work on a tribute page for the first time. The issue at hand revolves around the responsiveness of the background image within the jumbotron class. Upon resizing the screen, only half of the image is ...

"Experience the power of utilizing TypeScript with the seamless compatibility provided by the

I'm attempting to utilize jsymal.safeDump(somedata). So far, I've executed npm install --save-dev @types/js-yaml I've also configured my tsconfig file as: { "compilerOptions": { "types": [ "cypress" ...

"Debugging a simple demonstration showcasing NodeJS, AngularJS, and Express

Currently, I am following an online tutorial to learn a new concept. I have reached the part where I need to display data using HTTP GET from the controller, while the sample data is stored in the server file. I managed to fetch the data from the controlle ...

Error: React cannot read property 'any' since it is undefined

I am brand new to React and currently in the process of building a small app by following a tutorial on Udemy. The app includes a form, and while trying to import the redux-form library into my project, I encountered the following console error: https://i ...

The dropdown menu component in ReactJS is malfunctioning

I'm currently working on a form that includes a select box which fetches data from the server and posts it back to the same server. I have implemented the select box component from ant design. Unfortunately, I've encountered an issue with the ha ...

When the mouse is moved to the UL LI list, the border color of the Top Hover is reverted back to default

Can you assist me with this issue? I need to modify the code below so that the border color remains blue while a user selects an item from the UL LI list. Here is an image showing the current problem: https://i.sstatic.net/DS7hO.png And here is a pictu ...

What causes the background to shift as I scroll?

Whenever I scroll, the background image text seems to stick around no matter where I move on the page. Is there a way to keep it fixed in one place? .bg-image { background-image: url(../construction/3.jpg); filter: blur(8px); -webkit- ...

The Firefox tab continues to load endlessly due to an embedded iframe

As I test an HTML page on Firefox Developer, I encounter an issue with an iframe. While the iframe works fine on Google Chrome, it causes the parent page to continuously load on Firefox. This leads to a degradation in performance after a few minutes. The i ...

Side-to-Side Bootstrap Display Panel

I have been attempting to customize a Bootstrap panel to create a horizontal version, but I am struggling to align the panel heading vertically with the content in the panel body. I believe it may be related to clearing the divs. Front-end development is ...

I need help extracting an array name from a JavaScript array. I think it might be a syntax problem, but I am new to programming

In the process of developing a program where a video plays, featuring a person speaking along with chunks of text appearing in a div as the words are spoken alongside their translation. As part of this project, I'm working on writing a JavaScript func ...

Is there a way to display a single Vue component in two separate sub routes simultaneously?

const routes = [ { path: '/', component: LogInView }, { path: '/store', component: Dashboard, children: [ { path: '/products', component: ProductsView, }, ] }, { ...

Integrate the elements from the <template> section into the designated <slot> area

I am trying to retrieve template content, insert it into a custom element with shadow DOM, and style the span elements inside the template using the ::slotted selector. However, it seems like this functionality is not working as I expected. <!doctype h ...

Angular - Implementing an Object Mapping Feature

Looking to dynamically generate parameter objects in Angular with a structure like this: [ password: {type: 'String', required: true, value: 'apassword'}, someRandomParam: {type: 'Integer', required: false, value:3} ] ...

What is preventing me from properly aligning these two divs side by side?

Can someone please assist me with aligning two images side by side in a footer using bootstrap? I have tried offsetting the columns but the images keep stacking on top of each other instead of next to each other. <div class="row"> <div cl ...

There is an irritating 5px gap between the divs in the Avada WordPress theme. While using margin-top: -5px helps to reduce this spacing, padding-top: -5

Struggling to remove a persistent 5px whitespace on my website eternalminerals.com See the issue highlighted in this screenshot: Trying to eliminate the whitespace by adjusting margins in Google Chrome, but unable to apply styles to Avada shortcodes: &l ...