Solved

how to get previous values while do on change in dropdown

Posted on 2014-11-12
1
2,823 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
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now