?
Solved

how to get previous values while do on change in dropdown

Posted on 2014-11-12
1
Medium Priority
?
4,502 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 2000 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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

752 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