Ask your JavaScript questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

add a push record to toSort and then use ratingSort() JavaScript

  • REFUNDED

I need to add a record to a json file (toSort) from a form. Sort toSort (as below). Add a new <li> item to <ul id = "sortable_1">. Redraw the <ul id = "sortable_1"> with ratingSort().

https://verlager.com/dev/pairing.php?box=12

HTML:

<form onsubmit="add_list();" method="post" name="late_to_party" class="modal" autocomplete="off" id="ex2" style="display:none; background:lightgreen; width:40rem; padding:2.5rem">
<fieldset>

<label style = "font-size:1.5rem; width:10rem; float:left">Last, First</label>
<input type="text" class = "text" maxlength = "25" size = "20" name="name" id="wood" style = "text-transform: capitalize; font-size:1.8rem; width:25rem; float:left" autofocus />

<br /><input type="submit" style="display: none" />

<label style = "font-size:1.5rem; width:8rem; clear:both; float:left; margin-left:2rem; margin-top:2rem">Rating</label>
<input type="text" class = "numbers" maxlength = "4" size = "4" id="chuck" style = "width:8rem; font-size:1.8rem;float:left; margin-top:2rem"><br />

<input type="submit" id = "add_me" value="{enter} to add new player" />
</fieldset>
</form>

<script>

JS:

limit = <?php echo $_GET['box']; ?>

function add_list() {
// Get name and rating
var moniker_name = document.getElementById("wood").value;
if (!moniker_name) {return;}
var moniker_rate = document.getElementById("chuck").value;
var nameValue = moniker_name + " " +moniker_rate;

// add name and rating to toSort
toSort.push({name: moniker_name, rate: moniker_rate });

//Sort toSort
toSort.sort(function(a, b){ return b.rate - a.rate;});

var ul = document.getElementById("sortable_1");
var li = document.createElement("li");
li.appendChild(document.createTextNode(nameValue));
ul.appendChild(li); limit++;
$(#sortable_1).empty();
ratingSort(); // THIS DOESN'T WORK !

var form = document.getElementById("ex2"); form.reset();

}
</script>


<script> <!-- by Rainner Lins @ javascriptquestions.com -->

// this will select the list above, gerenate the items and place them in the list
let list = document.querySelector('#sortable_1');
let items = '';
let board = 0;
let limit = toSort.length;

for (let i = 0; i < limit; i++) {

items += `<li class="ui-state-default" id="I${i}">Item ${i}</li> \n`;

}

list.innerHTML = items;

</script>

<!--SORT BY RATING -->

<script>

function ratingSort() { $("li").css("border-color", "black");

for (i = 0; i <= limit;) {

var fullRating = toSort[i].rate;

var Name = toSort[i].name.split(" "); var lastName = Name[0]; var firstName = Name[1];

var xLS = lastName.length; var xFS = firstName.length; var xTT = xLS + xFS;

$("#I" + (i)).text(displayName);
colorize($("#I" + (i)), toSort[i].rate);

i++;}

}

</script>

Answers (0)

No answers yet.