How do I restore a previous selected option on confirm dialog


Hi Experts!

I have the following problem. I have a drop-down option with a confirm dialog. When a user selects a particular value a confirm dialog pops.  However when I click cancel the value of the drop-down is not reseted to the previous user selected, it is just the new value.
Is there any way to "remember" the previous value and set it back when the user click Cancel?
dlcnetAsked:
Who is Participating?
 
hieloCommented:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
                <script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>

        <title>New Web Project</title>
    </head>
    <body>
        <h3>New Web Project Page</h3>
                
                <select id="dropDown1">
                        <option value=""></option>
                        <option value="1">Option1</option>
                        <option value="2">Option2</option>
                        <option value="3">Option3</option>
                        <option value="4">Option4</option>
                        
                </select>
                
                <script type="text/javascript">
                        var selectedValue=0;
                        jQuery(function () {
                                
                                jQuery('#dropDown1').change(function () {
                                        //var selectedValue = jQuery('#dropDown1').val();
                                        //alert(selectedValue);                                 
                                        
                                        var confirmText = "Do you want to proceed?";                                    
                                        if ( jQuery('#dropDown1 option:selected').val() == "") {
                                                if (confirm(confirmText)) {
                                                        jQuery('<a>jQuery</a>').attr('href', 'www.jquery.com').appendTo('body');                
                                                } else {
                                                        this.selectedIndex=selectedValue;
											 return false;
                                                }
                                        }
								else
								{
									   selectedValue = jQuery('#dropDown1 option').index($('#dropDown1 option:selected'));
								}
                                })                              
                        });
                        
                </script>
                
    </body>
</html>

Open in new window

0
 
hieloCommented:
at load time, save the "default" value onto a global variable. Upon selection change, update update that variable only if user does not cancel. So when the dialog is dismissed by cancelling, then use use the value in that variable to reset to the previous selection.
0
 
AveshCommented:
Make a printout when you done.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
dlcnetAuthor Commented:

Hi Hielo

I tried that. However my drop-down has 25 options and only 1 has the confirm dialog. At any change the "global variable" is updated with the new value selected.
0
 
hieloCommented:
post a link to your page.
0
 
dlcnetAuthor Commented:

Hi!

The code I posted bellow. When the user selects nothing then the confirm dialog comes. However I would like when he No/ Cancel chooses to "remember" the previous selection that the user had and "restore" it
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<script type="text/javascript" src="lib/jquery/jquery-1.3.2.js"></script>

        <title>New Web Project</title>
    </head>
    <body>
        <h3>New Web Project Page</h3>
		
		<select id="dropDown1">
			<option value=""></option>
			<option value="1">Option1</option>
			<option value="2">Option2</option>
			<option value="3">Option3</option>
			<option value="4">Option4</option>
			
		</select>
		
		<script type="text/javascript">
			
			jQuery(function () {
				
				jQuery('#dropDown1').change(function () {
					var selectedValue = jQuery('#dropDown1').val();
					//alert(selectedValue);					
					
					var confirmText = "Do you want to proceed?";					
					if (selectedValue == "") {
						if (confirm(confirmText)) {
							jQuery('<a>jQuery</a>').attr('href', 'www.jquery.com').appendTo('body');		
						} else {
							return none;
						}
					}				
				})				
			});
			
		</script>
		
    </body>
</html>

Open in new window

0
 
dlcnetAuthor Commented:
A A++++ solution
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.