I found some examples in SO. But none of them worked for me. I want to swap values of 2 @Html.DropDownListFor
each other. Both DropDownList contains the same set of values except the default value to show. Below is my code.
<div class="currency_swap">
<p>
<label>From</label>
<div class="swap_dropdown">
@Html.DropDownListFor(m => m.SourceCurrencyname, new SelectList(ViewBag.CurrencyNames, "Currency_Code", "Currency_Code"), "---- All ----")
</div>
</p>
<div><input type="button" id="swap_up" value="▲" class="swap" /></div>
<p>
<label>To</label>
<div class="swap_dropdown">@Html.DropDownListFor(m => m.TargetCurrencyname, new SelectList(ViewBag.CurrencyNames, "Currency_Code", "Currency_Code"), "Agency Currency")</div>
</p>
</div>
<script type="text/javascript">
$(".swap").click(function () {
var row = $(this).closest(".currency_swap");
var start = row.find("swap_dropdown:first select");
var end = row.find("swap_dropdown:last select");
var temp = start.val();
start.val(end.val());
end.val(temp);
});
</script>
The start
and end
variables are not correctly set. The selectors could be something like the following.
var start = row.find(".swap_dropdown select:first");
var end = row.find(".swap_dropdown select:last");
The rest of the code worked, see js-fiddle here.