Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Get dropdown previous value in jquery

Posted on 2011-02-17
4
Medium Priority
?
1,058 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
[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
  • 2
4 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
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 2000 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

721 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