Solved

Get dropdown previous value in jquery

Posted on 2011-02-17
4
1,021 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to escape unsafe HTML tags 4 36
How Close unsubmited attempts 10 42
what should i learn? javascript or typescript? 3 33
Glitching Slide Show 19 26
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

713 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