To teach myself Javascript, I'm trying to make a web page that gives users a list of items (e.g. foods), asks them to sort these foods from favorite to least favorite, and submit the data when they're done. Using jQuery sortables seems like a good way to do this. However, I'm not sure how the data submission should happen.
Here's what I'm thinking. Each of these food items would be in a div like this:
<div id="sortable">
<div id="1" class="foods">Pizza</div>
<div id="2" class="foods">Sushi</div>
<div id="3" class="foods">Taco</div>
</div>
When the user clicks a "submit" button, I want the order of these items to be determined, and for this ordering to be sent back to the server (by the way, I'm using Django on the server side). It seems I can determine the order of the items with a function like this:
function getOrder()
{
var foods = $(".foods");
var ids = [];
for(var x=0; x<foods.length; x++)
{
ids.push(foods[x].id);
}
return ids;
}
However, I'm stuck on a couple of things:
- Where in my code would I call this function? I'm thinking it would be an onclick action when the user presses the submit button, but I'm not sure where the data the function returns would get passed.
- What format would be the most appropriate for sending this ordering to the server (e.g JSON)?
(I know this is a really basic question, but I have never made a web page with JavaScript before, so this area of programming is all new to me.)