4

I'm new to javascript and starting the mix javascript + jquery + coffeescript all together is not easy for a newbie like me...

I've created a very simple sortable list and I'd like to renumber my list on the fly (the 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 generates 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'm trying to renumber what's inside the span.number elements when the update callback triggers but I get following error message:

element is not defined

Any help would be very welcome! Thanks!

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

$('span.number').each (i, element)  =>
      $(element).html i

1 Answer 1

4

I don't know coffee script but generally using jQuery selector doesn't require the full path. e.g. $('tr > td > a > span.number') could be rewritten as $('.number'), also the .each() is generally used as .each(function(index, element) { YOUR CODE });. The last thing that looks out of place is setting the html this is generally done as .html('value'). So in your case $(element).html(i);. Hope this helps?

Sign up to request clarification or add additional context in comments.

4 Comments

Hello, you're right about the selector full path. I changed it. Thanks. About the remaining, I think it's coffeescript syntax equivalent to what you say in javascript. But there is definitely something wrong somewhere...
If you read the code, logically a post occur and directly after that you do a each. It is possible that due to the sequence of events, the post is done and the dom is not completely created. Try moving the .each fucntionality to a $(document).ready(function(){}); or coffee script equivalent. Thus you know that the script will always execute once the post is completed and the dom was created. It still happens before the render?
Guess what? The code was ok. It was only a matter of indentation... I discovered this when I moved the code according to your suggestion. Many thanks!
Can you update the question to reflect the actual issue? It will be good education for others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.