Solved

how to get previous values while do on change in dropdown

Posted on 2014-11-12
1
4,147 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

726 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