Solved

how to get previous values while do on change in dropdown

Posted on 2014-11-12
1
3,339 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
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)

803 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