I was doing a bunch of rjs stuff in rails, and lots of ajax goodness, and found myself constantly showing, and hiding the spinner and the update element (like a submit button, or other button)..
I’m a bit of a minimalist where possible and like to get rid of as much code as I can, and being as DRY as I can, and so I decided to make the following function:
- function updating_field(field_name, finished) {
- if (finished) {
- $(‘spinner‘).hide();
- $(‘update_‘ + field_name).show();
- } else {
- $(‘spinner‘).show();
- $(‘update_‘ + field_name).hide();
- }
- }
I simply call this method from the onclick method, or sumbit method of elements on the page and it tidies up my code considerably.
- <input type="submit" value="Save these changes" id="update_avatars2" onclick = "updating_avatars(false);" />
I can then call this code over and over again… In fact, I do, and I have many other helpers similar to this – which I’ll explore in other blog posts.

RSS Feed
Nice blog, but could you please change the colour of your code snippets? The parameters for the HTML snippet are in dark blue and almost unreadable.