Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Javascript JSON
Dear Sir,
 
I need, How to get previous selected item and
current selected item from
drop down list using Java script?

 
Note:
Both of the values i need. How to solve this problem in javascript?????
 

 
By MohaN
Posted 11-Apr-12 19:55pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

To get the current selected item:
var dropdown = document.getElementById("DropDownListID");
var selValue = dropdown.options[dropdown.selectedIndex].value;
 
To get the previous value, it's easy to get them using jQuery:
get previous selected dropdown item[^]
Getting value of select (dropdown) before change[^].
I am not sure of a direct way to get the previous value using JavaScript. But if you have to do it using JavaScript, use couple of hidden fields and store previous and current value in them. When the dropdownlist value is changed, copy current hidden field value to prev. hidden field value and then replace current hidden field value with selected drop down value. In this way you will have both the values. I am using to hidden field because I am not sure if onbeforechange event works for drop down list. Check out if it does, it would be much easier.
The other way would be to try and break the jQuery method to JavaScript. Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can achieve this by following way
 
<script type="text/javascript">
    function GetPriviousValue()
    {
        var currentValue = $(".ddlList").val();
        var previousValue = $("#hdnPreviousValue").val();
 
        if (previousValue != "")
        {
            alert("CurrentValue : " + currentValue + " PreviousValue : " + previousValue);
        }
        else
        {
            alert("CurrentValue : " + currentValue + " And FirstTime Dropdown Value is selected.");
            $("#hdnPreviousValue").val(currentValue);
        }
    }
</script>
<input type="hidden" id="hdnPreviousValue" />
<asp:dropdownlist id="ddlList" cssclass="ddlList" runat="server" xmlns:asp="#unknown">
<asp:listitem value="abc" text="abc"></asp:listitem>
<asp:listitem value="xyz" text="xyz"></asp:listitem>
<asp:listitem value="pqr" text="pqr"></asp:listitem>
<asp:listitem value="asd" text="asd"></asp:listitem>
<asp:listitem value="ytr" text="ytr"></asp:listitem>
<asp:listitem value="mnb" text="mnb"></asp:listitem>
<asp:listitem value="uio" text="uio"></asp:listitem>
</asp:dropdownlist>
<input type="button" id="btnOK" onclick="GetPriviousValue()" />
 
Try this one, It is definitely work for you.
  Permalink  
Comments
TecNQ at 3-Sep-13 18:32pm
   
I had to change the function to receive the ddl as a "this" so I could use it directly. It was not working, when using $(".ddlList").val();
 
So my function is like this ...
 
..
..
<input type="hidden" id="hdnPreviousValue" />
<asp:DropDownList ID="ddlAbsence" runat="server" onChange="ddlChanged(this);">
..
..
 
..
..
function ddlChanged(ddl)
{
var currentValue = ddl.selectedIndex;
var previousValue = $("#hdnPreviousValue").val();
..
..
//Update hidden field with current value from previous value
$("#hdnPreviousValue").val(currentValue);
}

But overall this worked for me. All other "solutions" I found using jQuery was confusing as hell. This is much simpler. Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 459
1 Maciej Los 325
2 Sergey Alexandrovich Kryukov 234
3 DamithSL 233
4 BillWoodruff 200
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 12 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100