Each loop in coffeescript, jquery

I am new to javascript and starting to mix javascript + jquery + coffeescript together is not easy for a beginner like me ...

I created a very simple sortable list, and I would like to renumber my list on the fly (server side code is ok).

The coffeescript code I wrote is:

jQuery -> $('.simple_grid tbody').sortable axis: 'y' containment: 'parent' cursor: 'move' tolerance: 'pointer' update: (event,ui)-> $.post($(this).attr('dataupdateurl') + '/' + ui.item.attr('id') + '/reorder/' + ui.item.index()) $('tr > td > a > span.number').each (i, element) => $(element).html i 

This creates a table of this kind

 <table class= "simple-grid"> <tbody dataupdateurl = "xxx"> <tr> <td> <a href="some_link"><span class="number">1</span>text 1</a> </td> <td> <a href="some_link"><span class="number">2</span>text 2</a> </td> <td> <a href="some_link"><span class="number">3</span>text 3</a> </td> </tr> </tbody> </table> 

I am trying to renumber what is inside the span.number elements when the update callback fires, but I get the following error message:

Element

indefined

Any help would be greatly appreciated! Thanks!

UPDATE: the problem was due to the fact that I missed the indentation in the last function:

 $('span.number').each (i, element) => $(element).html i 
+4
source share
1 answer

I do not know the coffee script, but as a rule, using the jQuery selector, the full path is not required. for example $('tr > td > a > span.number') can be rewritten as $('.number') , also .each () is usually used as .each(function(index, element) { YOUR CODE }); . The last thing that looks out of place is installing html, this is usually done as .html('value') . So in your case $(element).html(i); . Hope this helps?

+4
source

All Articles