Click here to Skip to main content
16,012,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

To get the current selected item:
JavaScript
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. :)
 
Share this answer
 
You can achieve this by following way

JavaScript
<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>

C#
<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.
 
Share this answer
 
Comments
Fandango68 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900