Solved

Get dropdown previous value in jquery

Posted on 2011-02-17
4
996 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now