473,396 Members | 1,921 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,396 software developers and data experts.

how to keep the selected values in the drop down list using javascript

Hi, Here in the below code i need to keep the selected value to be shown. But if i select and update the page the list box always shows the index value "select".I want it to show the selected value. what changes i have to make in the below code



Expand|Select|Wrap|Line Numbers
  1. <select name="year" id="year<%=gfimCount%>"  class="textbox" style="width:60px;" >
  2.   <option value="select">Select</option>
  3.           <script type="text/javascript">
  4.            var YearList = 6; //  number of "year" selections.
  5.            var year = new Date().getFullYear();
  6.                 for(var i=5;i>0;i--){
  7.                var previousyr=year-i;
  8.                document.write('<option value="' + previousyr + '">' + previousyr + '</option>'); 
  9.                 }
  10. //document.write('<option selected="true" value="'+previousyr+'">'+previousyr+'</option>');
  11.                for(var i=0;i<YearList;i++){
  12.                var currentyr = year + i;    
  13.                document.write('<option value="' + currentyr + '">' + currentyr + '</option>');     //document.getElementBbyId(<%="currentyr"%>).value;
  14.                }
  15. //document.write('<option selected="true" value="'+currentyr+'">'+currentyr+'</option>');
  16.         </script>
  17. </select>
Jan 13 '15 #1
1 3167
Dormilich
8,658 Expert Mod 8TB
But if i select and update the page the list box always shows the index value "select".
naturally, JavaScript is reset on every page load. (otherwise you could do some nice hacking attacks (like a key logger))

what you can do though is saving the selected value in a session (cookie, WebStorage) and load it off that store on every page load.
Jan 13 '15 #2

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

Similar topics

2
by: Serge Myrand | last post by:
Hi, I cannot get the selected option to be POSTed (it does not appear in the QueryString when using GET neither) I construct the drop down list from an array, I select an element in the list,...
4
by: charliewest | last post by:
I need to set the selected drop down list value at run time. I am aware of the method "SelectIndex" however this works only if you know the precise location of the value within the ListItem...
1
by: craig | last post by:
Hi, I've been asked to create a page that dynamically populates the contents of related drop down lists using JavaScript, and then obviously posts things back to the server. For completeness,...
6
by: nishtr | last post by:
Can any one told me that how can i format the drop down list using CSS
2
by: subhashk | last post by:
Is it possible to create editable drop down list using ASP.NET?
1
by: subhashk | last post by:
Is it possible to create a editable HTML drop down list using javascript?
3
by: penny111 | last post by:
Hi there, For my application, i need to have 3 drop down lists 1. drop down list of folder names 2. drop down list of documents in the folder selected 3. drop down list of instances of the...
2
by: crazychrisy54 | last post by:
Hi there I have a option, select drop down list which a user can open. My page however refreshes very frequently and when this occurs the selected drop down list will pop back up. The user then...
1
by: impin | last post by:
Form: <form name="newuserForm" action="register.php" method="post" onsubmit="return validateForm()";> <label style="font-size:18px">Gender<br /> </label><label style="font-size:18px">Male...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.