Solved

how to get previous values while do on change in dropdown

Posted on 2014-11-12
1
3,759 Views
Last Modified: 2014-11-18
how to get previous values while do on change in dropdown

eg:
in my drop down i selected as 4  then i going to change the drop down to 5
i want to get previous values (4) also .

some condition the new value is duplicate then i need to set the old (previous ) value
0
Comment
[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
1 Comment
 
LVL 8

Accepted Solution

by:
vr6r earned 500 total points
ID: 40439402
You'll need to save the previous value to a js variable since the normal change events occur after the new value has already been set.  Here's an example:

<select id="dropdown">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>

<script>
$(function(){
    var previous;
    
    $('#dropdown').focus(function(e){
       previous = $(this).val(); 
    });
    
    $('#dropdown').change(function(e){
        //use 'previous' var to perform whatever logic you need here
        alert("previous value = " + previous + ", current value = " + $(this).val());
        
        //set previous to current selection value now in case 
        //user makes another selection without the dropdown losing focus
        previous = $(this).val();
    });
});
</script>

Open in new window


fiddle: http://jsfiddle.net/6pg1L2nk/2/

Hope this helps.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
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…

696 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