473,549 Members | 4,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to keep the selected value in the dropdownlist

2 New Member
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 1538
jmrker
17 New Member
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
13302
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 page is refreshed. any ideas? Thanks in advance. Patrick
5
2656
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" runat="server" Width="100"></asp:dropdownlist> Sub RunReport_OnClick(sender As Object, e As System.EventArgs) _sqlStmt &= " AND tblSurvey1.clnGUID = '" &
6
5387
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 am receiving errors when trying to set the selected value of the dropdown within the ItemDataBound() subroutine. Below is the code I am using. I...
4
15976
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 can set the dropdownlist selectedindex. How do I set the correct value? private void lstDegrees_ItemDataBound(object sender,...
15
3798
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 still selected. So far so good. But the very weird thing is that if I look at the source code : <option selected="selected"...
1
10380
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 edit mode, everything works fine - the selected value in each list defaults to the value from the database record (selectedvalue=bind...). However,...
9
3406
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 the value of the DropDownList in the Page_PreInit event. How can I do that? The content in the content and master pages is based on
3
6578
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 dropdownlist which selected value should be the same as a label text from normal view mode <EditItemTemplate> <asp:DropDownList ID="ddlGrdvFrequency"...
0
1838
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 a list of countries. Any help would be greatly appreciated, thanks
0
1130
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
7526
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7962
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7480
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7814
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5373
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3504
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3486
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.