So I'm very new to the world of jQuery and while writing a simple code a faced a problem that I couldn't figure out the solution for. First of all, there's a function that updates the position of the form on the dropdown according to its index. And then there's another function that will change the position of the form on the page when a new position is selected on the dropdown list:
$(document).ready(initialRank);
function initialRank() {
var itemIndex = $(".template-detail-field").length;
$('.field-rank').each(function () {
$(this).empty();
var pos = $(".field-rank").index($(this))+1;
for (var i = 1; i <= itemIndex; i++) {
if (i == pos) $(this).append('<option value="'+ i +'" selected>' + i + '</option>');
else $(this).append('<option value="' + i + '">' + i + '</option>');
}
});
}
$(document).on("change", ".field-rank", function () {
$(".template-detail-field").each(function () {
var sel = $(this).find(".field-rank").val();
var pre = $(".template-detail-field").eq(sel - 1);
$(this).insertBefore(pre);
});
initialRank();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="template-detail-field">
<p> Testing stuff 1 </p>
<select class="field-rank">
<option value="1" selected>1</option>
</select>
</div>
<div class="template-detail-field">
<p> Testing stuff 2 </p>
<select class="field-rank">
<option value="2" selected>2</option>
</select>
</div>
<div class="template-detail-field">
<p> Testing stuff 4 </p>
<select class="field-rank">
<option value="4" selected>4</option>
As you can see, after changing position I call the function initialRank() so that it updates the positions displayed for each form accordingly. The problem is after I do this, the dropdown list won't accept any input, and by that I mean when I choose a new position on the list the form doesn't change its position nor does the new chosen position is selected.
But when I rewrite the initialRank() code inside the change() function everything works fine:
$(document).ready(initialRank);
function initialRank() {
var itemIndex = $(".template-detail-field").length;
$('.field-rank').each(function () {
$(this).empty();
var pos = $(".field-rank").index($(this))+1;
for (var i = 1; i <= itemIndex; i++) {
if (i == pos) $(this).append('<option value="'+ i +'" selected>' + i + '</option>');
else $(this).append('<option value="' + i + '">' + i + '</option>');
} });
}
$(document).on("change", ".field-rank", function () {
$(".template-detail-field").each(function () {
var sel = $(this).find(".field-rank").val();
var pre = $(".template-detail-field").eq(sel - 1);
$(this).insertBefore(pre);
});
var itemIndex = $(".template-detail-field").length;
$('.field-rank').each(function () {
$(this).empty();
var pos = $(".field-rank").index($(this)) + 1;
for (var i = 1; i <= itemIndex; i++) {
if (i == pos) $(this).append('<option value="' + i + '" selected>' + i + '</option>');
else $(this).append('<option value="' + i + '">' + i + '</option>');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="template-detail-field">
<p> Testing stuff 1 </p>
<select class="field-rank">
<option value="1" selected>1</option>
</select>
</div>
<div class="template-detail-field">
<p> Testing stuff 2 </p>
<select class="field-rank">
<option value="2" selected>2</option>
</select>
</div>
<div class="template-detail-field">
<p> Testing stuff 4 </p>
<select class="field-rank">
<option value="4" selected>4</option>
Can someone please explain to me what I was doing wrong. Thank you.