Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8190169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:31:55+00:00 2026-06-07T03:31:55+00:00

I am trying to get the year from a javascript created html select however

  • 0

I am trying to get the year from a javascript created html select however the javascript function that is supposed to read the data can’t find it.

Here is the code for index.html

<html>
<head>
    <SCRIPT language="JavaScript" SRC="./js/calendar.js"></SCRIPT>
</head>

<body onload="yearList()">
    <div id="form">
        <table>
            <form name="cal_form">
                <tr>
                    <td>Month:</td>
                    <td>
                        <select name="month">
                            <option value="January">January</option>
                            <option value="February">February</option>
                            <option value="March">March</option>
                            <option value="April">April</option>
                            <option value="May">May</option>
                            <option value="June">June</option>
                            <option value="July">July</option>
                            <option value="August">August</option>
                            <option value="September">September</option>
                            <option value="October">October</option>
                            <option value="November">November</option>
                            <option value="December">December</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Year:</td>
                    <td id="yearoptiondiv">

                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <button type="button" onclick="drawCalendar(gen_cal_settings())">Draw Calendar</button> 
                    </td>
                </tr>


            </form>
        </table>
    </div>
    <div id="calendar"></div>
</body>

And here is the code for calendar.js

function drawCalendar(cal_settings){
var calendarTable;

calendarTable += '<table>';
calendarTable += '<tr>';
calendarTable += '<td colspan="2">';
calendarTable += cal_settings["month"] + " " + cal_settings["year"];
calendarTable += '</td>';
calendarTable += '</tr>';
calendarTable += '</table>';
document.getElementById("calendar").innerHTML=calendarTable;
}

function yearList(){
var x="",i;
x += "<select name='year'>";
for (i=2011;i<=3012;i++)
{
    x += "<option value='" + i + "'>" + i + "</option>";
}
x += "</select>";
document.getElementById("yearoptiondiv").innerHTML=x;
}

function gen_cal_settings(){
var cal_settings = new Array(); 
cal_settings["month"]=document.forms['cal_form']['month'].value;     
cal_settings["year"]=document.forms['cal_form']['year'].value;   
return cal_settings;    
}

The Year list is ran in the onload event for the body of the page.
What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:31:57+00:00Added an answer on June 7, 2026 at 3:31 am

    You are accessing the value of the select, when the select contains an array of options. What you need is the value of the selected option, not the value of the select.

    You’d want something like:

    document.forms["cal_form"]["year"].options[document.forms["cal_form"]["year"].selectedIndex].value
    

    But man that is ugly. You should try to implement what Benjamin Gruenbaum has suggested and move this code into a handler that allows you to better maintain (and read) the Javascript you’re writing which will allow you to reference the form instead of accessing it from Document every time you need data from it.

    EDIT

    I tossed your code into a JSFiddle and played with it. What I can see is that your select is not part of the form object that is being returned from document.forms["cal_form"], what this tells me is that you need to generate the entire form via Javascript or you need to change the way you access the element, perhaps by id instead of by name (using document.getElementByID("id").

    I also recommend not using “innerHTML” to add a string of built HTML. I recommend building the elements through the DOM, an example being your yearList function to something like the following:

    function yearList(){
      var select, option, year;
      select = document.createElement("select");
      select.setAttribute("name", "year");
      select.setAttribute("id", "year"); // For use accessing by ID and not name
      for (i = 2011; i <= 3012; ++i)
      {
        option = document.createElement("option");
        option.setAttribute("value", year);
        option.innerHTML = year;
        select.appendChild(option);
      }
      document.getElementById("yearoptiondiv").appendChild(select);
    }
    

    And then when you need to access the value:

    document.getElementById("year").value;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get a date that is one year from the date I
I'm trying to get data from two tables. Here is my code: select p.nim,
i trying to get two date from two calender with jquery how can i
Trying to get a subset of a data frame based on, to borrow from
Im trying to get the Day / Month / Year from my datepicker. I
I'm trying to get data from a database using FMDB but having tried with
I'm trying to get one year from now's date, and it's not working. JS:
I'm trying get the visible portion of UIImage from an UIImageView . UIImageView takes
I am trying get all html links within a string and replace them using
I am trying get all html links within a string and replace them using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.