Solved

Get dropdown previous value in jquery

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
javascript: add id amounts 5 47
Need Help adding an array and loop to a React file 7 28
where is team.js? 3 26
Jquery-Javascript setAttribute function help 7 10
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

777 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