Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

populate year drop down list

Posted on 2010-11-20
9
Medium Priority
?
454 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

715 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