problem with selection of date and month in dropdown list with jquery

Recently, I've been dabbling in jQuery and decided to create two list boxes using it. One box contains dates while the other contains months. It's quite handy since I need them in different sections of my HTML page. However, when attempting to encapsulate this functionality within a function, things didn't go as planned. Here is the link to the code on JSFiddle

      <div class="drop">
       <div style="float:left;">Date of Birth : &nbsp;</div>
       <div class="current"><input type="text"  name="Mem_DOB" class="memdate1"/></div>
     <div class="alldate1"></div> <div class="allmonth1"></div>
    </div> 

      <div class="drop">
       <div style="float:left;">Wedding Anniversery : &nbsp;</div>
       <div class="current"><input type="text"  name="Mem_WAnn" class="memdate2"/></div>
     <div class="alldate2"></div> <div class="allmonth2"></div>
    </div>


$(document).ready(function () {
 $('.memdate1').click(function () {
    var date = $('.alldate1').attr('id');
    alert(date);
    var month = $('allmonth1').attr('id'); 
    GenerateCalendar(date, month);
});
$('.memdate2').click(function () {
    var date = $('.alldate1').attr('id');
    alert(date);
    var month = $('allmonth2').attr('id'); 
    GenerateCalendar(date, month);
});

function GenerateCalendar(date,month) {
    var arr = []
    for (var i = 1; i < 32; i++) {
        arr.push({ val: i, text: i })
    }
    var sel = $('<select>');
    $(arr).each(function () {
        sel.append($("<option>").attr('value', this.val)
            .text(this.text));
    });
    $('.div'+ date).html(sel);
    $('.div'+ date).show();
    //month list
    var arr = [
        { val: 'January', text: 'January' },
        { val: 'February', text: 'February' },
        { val: 'March', text: 'March' },
        { val: 'April', text: 'April' },
        { val: 'May', text: 'May' },
        { val: 'June', text: 'June' },
        { val: 'July', text: 'July' },
        { val: 'August', text: 'August' },
        { val: 'September', text: 'September' },
        { val: 'October', text: 'October' },
        { val: 'November', text: 'November' },
        { val: 'December', text: 'December' },
    ];
    var sel = $('<select>');

    $(arr).each(function () {
        sel.append($("<option>").attr('value', this.val)
            .text(this.text));
    });

    $('.div' + month).html(sel);
    $('.div' + month).show();
    //$('.memdate').hide();$("div#" + name).
}

});

Answer №1

Substitute

let number = $('.allnumbers').attr('id');

using

let number = $(this).val();

since the ID attribute is not specified for HTML elements.

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

Unchecking random checkboxes within a div using jQuery after checking them all

Once a link is clicked on, all checkboxes within that particular div will be checked. function initSelectAll() { $("form").find("a.selectAll").click(function() { var cb = $(this).closest("div").find("input[type=checkbox]"); cb.not(":checked" ...

Adding the "input-invalid" class to the input doesn't take effect until I click outside of the input field

When the zipcode field is updated, an ajax call is made. If there are no zipcodes available, I want to mark it as invalid by adding the "input-invalid" class. However, the red border validation only appears when clicking outside of the input field. Is ther ...

Enhance your textbox with more detailed descriptions than just displaying NaN

I am working on a form that includes select boxes. If a user selects an option from "Convert From" and another option from "Convert To" but does not enter a number in the input field, instead of displaying NaN in the result text box, I would like to show ...

Tips for navigating the HTML DOM without using window.scrollBy(x, y) (specifically for scrolling within an element)

Desiring to scroll down along with my selected document, I experimented with the following code. window.scrollTo(x, y); const body = document.getElementsByClassName("body")[0]; body.scrollTo(x, y); However, there are instances where it returns "undefined ...

Title with lower border shorter than width

Looking to achieve an underline effect with a bottom border that is narrower than the width of the h2 title. Though I typically avoid uploading images, here is one to provide further clarification of my question: ...

Is there a way to increase the values of my dropdown menu options by selecting a different option in another dropdown menu?

I have a pair of select boxes: <select class="years"> <option value="1">1 year</option> <option value="5">5 years</option> </select> <select class="factor"> <option value="1">normal</option> & ...

SVG image element in Firefox does not respond to hover effect

I am currently working on developing an engaging SVG map. My goal is to have the image elements increase in size from 20px to 50px when hovered over. Below is an example of what I'm aiming to achieve: <svg xmlns="http://www.w3.org/2000/svg&quo ...

CSS Vertical Accordion Menu

I am struggling to modify the vertical accordion menu CSS. I am having difficulty adjusting the sub-menu list items. <div id="menuleft"> <div class="top">header</div> <ul> <li><a href="#">Main 1</a> ...

Update the query string in the src attribute of the image tag

I'm facing a challenge with our products loading at specific sizes and quality that I need to address. I am looking to update a portion of the img src code: &wid=128&hei=144&qlt=80 to: &wid=256&hei=288&qlt=100 I have attemp ...

Is there a way to adjust the width of my editor file in vscode to prevent the need for horizontal scrolling when reading a paragraph to the end?

My lorem ipsum text in vscode is displaying as one long line, forcing me to scroll horizontally instead of seeing it formatted into paragraphs. https://i.stack.imgur.com/LIja8.png Is there a way to adjust the width of the editor box to prevent the need f ...

Making the Bootstrap 4 card-footer expand to occupy the remaining height of the column

I'm currently developing a project that incorporates bootstrap cards. There are 3 cards, each for a different column, and I need them to have equal heights. Here is how they currently look: The B and C cards should occupy the remaining height of thei ...

Linking jQuery UI tabs to tabs on a different pageWould you like assistance

I am facing a challenge that I need help with. For a school project, I have to create 8 pages of HTML, each with a template and a menu. The menu consists of tabs that are supposed to link to separate pages when clicked. However, the issue is that when I cl ...

Transfer information to the form through a hyperlink

I need help with a script that sends data to a form when a link is clicked. What I'm trying to achieve is to have the data appear in the form when the user clicks the link below, which is generated from a database. <div id="menu_bar" region="west ...

Three responsive images neatly arranged within separate div containers

Looking to have these 3 divs align horizontally with responsive images. Having trouble maintaining the layout when setting max-width. Any suggestions? .fl { display: flex; flex-wrap: no-wrap; height: 35%; flex-direction: row; } .pic { width: ...

Exploring the scope of event listeners in jQuery's TimeCircles

Currently experimenting with the jquery timer known as "TimeCircles", which can be found here. I have successfully set up a minute / second timer for 30 minutes with an alert that triggers at 25 minutes. My goal is to implement an event when the timer hits ...

Navigating Websites in Google Search

Whenever I search for keywords related to my website on Google, the search results show my website's content and address. However, when I click on the link, it redirects me to a different unrelated website. Click here to view an image ...

Exploring the process of assigning responses to questions within my software program

I am looking to display my question choices as radio buttons in a modal window. I have tried several solutions without success. Here is my question module: import questions from "./Data"; const QuestionModel = () => { return ( <div cl ...

When located at the bottom of a page, the Div element fails to display

I need some help with my javascript code. I am working on creating a scrollable panel that closes when scrolled and reveals a #toTop div when the bottom of the page is reached. Below is the function code I have written, but for some reason, the #toTop div ...

Excessive content spilling over the div container

Hello everyone I've been trying to set up a div that includes an image, a title, and some text. I want the image to be aligned on the left side, with the title and text following on the right. However, I'm having an issue where the text is overf ...

Modifying the input field's name attribute while utilizing multiple datasets in typeahead.js

I am currently utilizing typeahead.js with multiple datasets, following the guidance provided here. I have chosen not to employ Bloodhound in my implementation, resulting in some differences in my code structure, outlined below: $('#search .typeahead ...