473,395 Members | 1,675 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how to keep the selected value in the dropdownlist

Expand|Select|Wrap|Line Numbers
  1. <option value="-select">Select</option> <script type="text/javascript">
  2. var YearList = 5; // Specify number of "year" selections.
  3. var year = new Date().getFullYear();
  4. var pryr1 = year-2;
  5. var pryr = year-1;
  6. document.write('<option value="' + pryr1 + '">' + pryr1 + '</option>'); 
  7. document.write('<option value="' + pryr + '">' + pryr + '</option>'); 
  8. for(var i=0;i<YearList;i++){
  9.     var curyr = year + i;
  10.     document.write('<option value="' + curyr + '">' + curyr + '</option>'); 
  11. }
  12. </script>
  13.  
by having this code the result shown in the drop down list is
2013
2014
2015
2016
2017
2018
2019
if i select any year over here and update the page, the value in the drop down box comes and again to "select"
i want the year which i selected to be shown
Jan 6 '15 #1
1 1534
jmrker
17
Don't use "document.write()" to create your drop-down selections.
As soon as you re-enter the function, it reloads the page to it's original contents.

Modify the following to meet your needs or come back with questions.

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title> Range of Years </title>
  6.  
  7. </head>
  8. <body>
  9.  From: <select id="SBox1" onchange="initBox2('SBox2',this.value)"></select>
  10.  To: <select id="SBox2"></select>
  11.  
  12. <script type="text/javascript">
  13. // From: http://www.dreamincode.net/forums/topic/341931-double-combo-box-year-range-problem/
  14.  
  15. function createRange(min, max) {
  16.     var range = [];
  17.     for (var i = min; i <= max; i++) { range.push(i); }
  18.     return range;
  19. }
  20. var StartYear = [];
  21. var EndYear = [];
  22. var date = new Date();
  23.  
  24. function initBoxes(sbox,arr) {
  25.   var Level1=document.getElementById(sbox);
  26.   Level1.options.length = 0;
  27.   for (i=0; i<arr.length; i++) {
  28.     var x=document.createElement('option');
  29.     var y=document.createTextNode(arr[i]);
  30.     if (window.attachEvent) { x.setAttribute('value',y.nodeValue); }  // for IE
  31.     x.appendChild(y);
  32.     Level1.appendChild(x);
  33.   }
  34. }
  35. function initBox2(sbox,newStart) {
  36.   EndYear = createRange(newStart,date.getFullYear()+1).reverse();  // plus next year
  37. //  EndYear = createRange(newStart,date.getFullYear()).reverse();  // to current year
  38.   initBoxes('SBox2',EndYear);
  39. }
  40. window.onload = function() {
  41.   StartYear = createRange(1925,date.getFullYear()+1).reverse();
  42. //  StartYear = createRange(1925,date.getFullYear()).reverse();
  43.   initBoxes('SBox1',StartYear);
  44. }
  45. </script>
  46.  
  47. </body>
  48. </html>
  49.  
  50.  
Jan 6 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Patrick | last post by:
Hello, I have a drop down list that when a value is selected the page refreshes itself but the selected value changes back to the default value. I would like to keep the selected value after the...
5
by: DC Gringo | last post by:
I have a dropdownlist that, upon form submission, I'd like to maintain the selected value when I get my result...how do I do that? <asp:dropdownlist Font-Size="8" id="ddlCommunities"...
6
by: Jenna Alten | last post by:
I have a datagrid with a template column that contains a dropdown list. I currently fill and display the dropdown list on the page load. This is working correctly. I am NOT using an Edit Column. I...
4
by: Chris Kettenbach | last post by:
Good morning all, I am sure this has been asked but I did not see anything. I have a datalist control. In the edititemtemplate I have a dropdownlist. I know on the itemdatabound event is where I...
15
by: sam | last post by:
Hi, I have a bound dropdownlist with an event handler OnSelectedIndexChanged. If I select a value I'm redirected to another page. I come back using the browser's back button, and the option is...
1
by: Ben | last post by:
I have a formview with a few dropdownlists (software version, database version, etc). When a software version is selected, the database version dropdownlist updates itself accordingly. When in...
9
by: Lammert | last post by:
Good morning, I create an ASP.NET 2.0 web application. The situation: 1. One masterpage where the users can select an organisation in a DropDownList. 2. Different content pages. I will get...
3
by: podi | last post by:
Hi, I just wonder if there is a way to set a selected item in dropdown list which items are retreived from database without touching c# code ? I mean, i have in gridview, in edit mode one...
0
by: saiprex | last post by:
I have DropDownList control which is binded to SqlDataSource. The SqlDataSource gets the values from TABLE, it all works great. But how can i set the selected value for the DropDownList? The table is...
0
by: ahmedelbahar66 | last post by:
how to use view state to keep selected value of dropdownlist in user control displayed in masterpage. when postback occur thae selected value of dropdownlist is cleared i dont know the reason !
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.