Guest User

Untitled

a guest
Jan 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. # the old way, as I've seen a few times
  2. # to get to our data we'd do a string sub to remove "thing_", leaving only the id we care about
  3. # which works, but isn't very semantic, in my opinion
  4. <div class="thing" id="thing_<%= thing.id %>"> stuff goes here! </div>
  5.  
  6. # here's the HTML5 way
  7. <div class="thing" data-thing_id="<%= thing.id %>"> stuff goes here! </div>
  8.  
  9. # now we can find that ID very easily in jQuery
  10. # which would be really useful if we want to do an AJAX call using that particular id as a parameter.
  11. var thingId = $(this).attr(data-thing_id);
  12. # example ajax post with this variable:
  13. $.post("things/example_action/" + thingId);
  14.  
  15. # and we can use the CSS selector to find that element easily as well (again with jQuery)
  16. # this would be useful AFTER we make an AJAX call and want to update that particular element on the page.
  17. # updating our one specific element is better than re-rendering an entire section
  18. $(".thing[data-thing_id='<%= @thing.id %>']");
Add Comment
Please, Sign In to add comment