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