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

Is it possible to update only the inner text of all elements throughout a webpage?

Scenario After coming across a thought-provoking XKCD comic, I decided to craft a script that would bring the comic's concept to life: javascript:var a=document.getElementsByTagName('body')[0].innerHTML;a=a.replace(/Program(\w\w+ ...

The responsiveness of Bootstrap 5 columns in Chrome inspection appears to break after reaching medium size, yet functions properly in other areas

I am currently working with cards within a grid system, but I am encountering an issue with resizing when the column size is smaller than medium in the mobile view inside the inspect section. The width does not expand to 100%, regardless of how much I try ...

Module specifiers that are considered relative must always commence with either "./", "../", or just "/"

I need help with importing three.js. I have been following the documentation but I keep encountering an error message: Uncaught TypeError: Error resolving module specifier “three”. Relative module specifiers must start with “./”, “../” or “/ ...

Using jQuery to import an external script into a React JS project

I'm looking to integrate an external JavaScript file (using jQuery) into a ReactJS project. While I found some guidance on this page, I am still encountering errors. The external JS file is named external.js: $(document).ready(function() { docu ...

Issue with button vanishing upon clicking page forward in pagination for table

When I click the next button on the table pager, a button that is inline with the pager disappears. I'm not sure why this is happening. If you want to see my code, check out this JS Fiddle: $table.trigger('repaginate'); var pager = documen ...

Create a datastring using a JSON object

When receiving data in JSON format from an AJAX call, the following code is used to parse it: var parsed=JSON.parse(data); An example output could look like this: {"confirm_type":"contract_action","job_id":12,"id":7} To generate a dynamic data string f ...

What is the best way to position two divs side by side while maintaining their individual padding?

When I remove the padding as shown, the website functions correctly with two div columns next to each other. However, upon adding padding, the #right div shifts downwards. How can I ensure it works as intended with padding included? HTML: Two divs direct ...

Incorporating a new text tag

Is there a way to add a text label for every circle that can be rotated and have its color changed? I'm looking for a solution that doesn't involve JSON data, but instead retrieves the necessary information from somewhere else. var w = 3 ...

How to prevent the scroll bar from affecting a fixed div located at the top of the page

Check out this website: I am struggling with setting up a fixed menu div at the top and a content div below it on my site. I want the scroll bar to only apply to the content div and not the header div at the top, but I can't seem to figure out how to ...

What is the best way to ensure my php variable is easily accessed?

Recently, I've been working on implementing a timer and came across the idea in a post on Stack Overflow. <?php if(($_SERVER['REQUEST_METHOD'] === 'POST') && !empty($_POST['username'])) { //secondsDif ...

Displaying random divs and subsequently animating them downwards using JavaScript

I am in the process of creating a random appearing div that then falls down. Here is the code I have so far: $(document).ready(function(){ $('#test').animate({top: 80 + '%'},900); }); <div id="test" style="background:#98bf21;heigh ...

Adding an element to the second-to-last position in a list

In the context of a HTML unordered list <ul id = "master_key_list"> <li id="master_key_23" class="available_element">Fix the Peanut Butter</li> <li id="master_key_24" class="available_element">Focus on Price Sensitivity</li& ...

SoundCloud and Vimeo have the capability to link their players across separate tabs, and even between tabs and embedded players

I find it intriguing how SoundCloud and Vimeo are synchronized with their tabs, so that when you play something in one tab, the others pause. It's worth noting that this feature only works on SoundCloud when two instances of the website are open, wher ...

Utilizing DIV elements within table cells

Within this particular table, it is not only cell B that contains both a heading and content, but cells A and C as well. My effort to establish the heading and content using DIV elements has been somewhat successful. While I have achieved the desired font ...

Guidelines for retrieving a file from Amazon S3 through a web browser utilizing Python (and boto) on Google App Engine

I have a Python program running on Google App Engine using boto 1.9b that retrieves all keys from an S3 Bucket and formats the output into an HTML table. bucket_instance = conn_s3.get_bucket(bucketname) liste_keys = bucket_instance.get_all_keys() table = ...

What is the best way to align text in the middle on smaller devices like phones and iPads?

I have been working on a login screen and encountered a problem with the layout on smaller devices. Despite centering all the content perfectly on my computer's browser, when I inspect it using developer tools for smaller devices, everything shifts to ...

Splitting a td tag into multiple columns dynamically with Angular

I am attempting to dynamically split the table element into separate columns. My desired layout should resemble this: The values for name and surname are fixed at 1, but the values for subjects and grades can vary (there may be 2 or more columns). Below ...

Verify that all checkboxes have been ticked

Is there a way to verify if all the checkboxes labeled with class="abc" are chosen? I have to perform this verification whenever any of them is either checked or unchecked. Should I do it upon clicking or changing? ...

Android Browser is experiencing issues with returning JSON data consistently after making an AJAX call

When I make an ajax call to an API for JSON data, everything works perfectly with other browsers. However, when using the Android Browser, the response seems to act strangely. I have added a console log using weinre to catch the returned data. Can anyone h ...

What is the best way to choose content that has been clicked on?

How do I display only clicked content? Currently, I have a system where clicking a button shows the data in the nav tag. The code is written in views.py. class TopView(TemplateView): model = Data template_name = 'index.html' def get ...