Solved

populate year drop down list

Posted on 2010-11-20
9
450 Views
Last Modified: 2013-11-05
i need to populate a year drop down list with a blank option but cant quite get it.
function populateYearSelect()
{
	d = new Date();
	curr_year = d.getFullYear();

	//document.getElementById('ddlexpyear').options = new Option("x");

	for(i = 0; i < 9; i++)
	{
		document.getElementById('ddlexpyear').options[i] = new Option(curr_year+i,curr_year+i);
	}
}

Open in new window

0
Comment
Question by:vicomin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 34179722
check this page : http://www.w3schools.com/jsref/met_select_add.asp

var y = document.createElement('option');
y.text='';
var x=document.getElementById("ddlexpyear");
try { x.add(y,null); } catch(ex) { x.add(y); }
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 34179734
Handle this on the form side of things like:

<select name="year" onChange="populateYearSelect(this.form)">
<form name="">Select Year</form>

then the rest

</select>
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34179736
So :

	function populateYearSelect()
	{
		d = new Date();
		curr_year = d.getFullYear();

		addOption('', "ddlexpyear");

		for(i = 0; i < 9; i++)
		{
			addOption(curr_year+i, "ddlexpyear");;
		}
	}

	function addOption(text, dropdownId) {
		var y = document.createElement('option');
		y.text = text;
		var x = document.getElementById(dropdownId);
		try { x.add(y,null); } catch(ex) { x.add(y); }
	}

Open in new window


Test page :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
	function populateYearSelect()
	{
		d = new Date();
		curr_year = d.getFullYear();

		addOption('', "ddlexpyear");

		for(i = 0; i < 9; i++)
		{
			addOption(curr_year+i, "ddlexpyear");;
		}
	}

	function addOption(text, dropdownId) {
		var y = document.createElement('option');
		y.text = text;
		var x = document.getElementById(dropdownId);
		try { x.add(y,null); } catch(ex) { x.add(y); }
	}

</script>
</head>
<body onload="populateYearSelect();">
<select id="ddlexpyear"></select>
</body>
</html>

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:vicomin
ID: 34179866
leakim971 - that creates a blank entry in the ddl but when i run my validation it fails and when i submit to the 2nd page it shows the value as empty. if i view the page source it shows:




validation that the 1st option is not selected.

		if(document.orderform.ddlexpyear.value == "")
   		{
					alert('You must select another year.')
					document.orderform.ddlexpyear.focus();
					return false;
		}


page source in browser
<select name="ddlexpyear" id="ddlexpyear" class="ddl-exp" tabindex="15">
 
</select>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 34179872
So use :

addOption(' ', "ddlexpyear"); // there's a space inside

instead : addOption('', "ddlexpyear");
0
 

Author Comment

by:vicomin
ID: 34179893
no luck - the validation prompts even if a year is selected.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 34179904
ok, add : y.value = text;

and remove the space : addOption('', "ddlexpyear");
function addOption(text, dropdownId) {
		var y = document.createElement('option');
		y.text = text;
		y.value = text;
		var x = document.getElementById(dropdownId);
		try { x.add(y,null); } catch(ex) { x.add(y); }
	}

Open in new window

0
 

Author Comment

by:vicomin
ID: 34179953
got it with the validation
if(document.orderform.ddlexpyear.selectedIndex==0)

not sure why i forgot about the index - duh
0
 
LVL 82

Expert Comment

by:leakim971
ID: 34179974
>not sure why i forgot about the index - duh

You may be able to sue the value instead the selectedIndex
But you right use the selectedIndex is a good choice I think

Thanks for the points!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Slush on text 2 29
Table header must be on top 2 58
Why does my submit button remain disabled 3 36
Javascript not to let an input enter more or less 4 20
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question