Solved

Get dropdown previous value in jquery

Posted on 2011-02-17
4
1,016 Views
Last Modified: 2012-05-11
Hi,

How to get previous selected dropdown value in jquery.
I tried $(this).prev().val() --> but it returns me undefined.

below is my code.

 <%: Html.DropDownListFor(model => model.ViewModel.StyleId,Html.StyleSelectListItems(ViewData["HouseTypeID"].ToString()),new { style = "width: 100%;",Class = "homeStyle" })%>
                                   

when i change it fire below method

 $("#ViewModel_StyleId").change(function (e) {

}

i tried below code, but doesn't help me.

$(this).prev().val();
$("#ViewModel_StyleId).prev().val();
0
Comment
Question by:kravindra
  • 2
4 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 34914139
$(this).prev().val() will only give the previous sibling's value, not the previous value of drop down.

You need to store the old value somewhere so that next time you can just fetch it from there.

You can use stack here, so that any time the value is changed, it will be pushed to the stack
0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 500 total points
ID: 34914540

Are you after something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26827824.html</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

jQuery(document).ready( function () {

  var previousValue = $("#ViewModel_StyleId").val();

  $("#ViewModel_StyleId").change( function() {
    $("#previous").text( previousValue );
    previousValue = $("#ViewModel_StyleId").val();
  });

});

</script>
</head>
<body>

<select id="ViewModel_StyleId">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
</select>

Previous value: <span id="previous"></span>

</body>
</html>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 34915386
Another one using  : $(this).attr("selectedIndex")-1

Value : $("option:eq(" + () +  ")", this).val()
Text :  $("option:eq(" + ($(this).attr("selectedIndex")-1) +  ")", this).text()

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 34915388
bad copy cut, sorry :

Value :
$("option:eq(" + ($(this).attr("selectedIndex")-1) +  ")", this).val()

Open in new window

Text :  
$("option:eq(" + ($(this).attr("selectedIndex")-1) +  ")", this).text()

Open in new window

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

808 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