Solved

populate year drop down list

Posted on 2010-11-20
9
451 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

627 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