Form Element AJAX Spinner Attachment Using MooTools

By  on  

Many times you'll see a form dynamically change available values based on the value of a form field. For example, a "State" field will change based on which Country a user selects. What annoys me about these forms is that they'll often do an AJAX request to grab the states but wont show any indicator to the user that something is happening. We're going to use MooTools to add an AJAX spinner image next to form fields that perform such AJAX requests.

The XHTML

<select class="ajax">
	<option value="">-- Select a Site--</option>
	<option value="David Walsh Blog">David Walsh Blog</option>
	<option value="Script & Style">Script & Style</option>
	<option value="Band Website Template">Band Website Template</option>
</select>

<br /><br />

<input type="text" id="my-text" class="ajax" />

Add the "ajax" class (or any class for that matter) to the elements you'd like the spinner to display by.

The MooTools JavaScript

window.addEvent('domready',function() {
	//inject image
	var spinner = new Element('img',{
		src: 'move-spinner.gif',
		styles: {
			position:'absolute'
		},
		opacity: 0
	}).inject(document.body);
	//form element events
	$$('.ajax').each(function(el) {
		//get coordinates
		var coords = el.getCoordinates(document.body);
		//ajax request object
		var request = new Request({
			url: '',
			method: 'post',
			onRequest: function() {
				spinner.setStyles({
					left: coords.right + 10,
					top: coords.top + 5
				}).fade('in');
			},
			onComplete: function() {
				spinner.fade('out');
			}
		});
		//change event
		el.addEvent('change',function() {
			//ajax request
			request.send({
				data: {
					ajax: 1,
					value: el.get('value')
				}
			});
		});
	});
});

We first add the spinner image to the document but hide it right away. When any of the form elements with the "ajax" class changes value, we move the spinner image to the right of the element. Then we use Reqeust's onRequest and onComplete methods to show and hide the spinner. Simple!

Look forward to a jQuery version tomorrow.

Recent Features

  • By
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

  • By
    Creating Scrolling Parallax Effects with CSS

    Introduction For quite a long time now websites with the so called "parallax" effect have been really popular. In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a...

Incredible Demos

  • By
    Fx.Rotate:  Animated Element Rotation with MooTools

    I was recently perusing the MooTools Forge and I saw a neat little plugin that allows for static element rotation: Fx.Rotate. Fx.Rotate is an extension of MooTools' native Fx class and rotates the element via CSS within each A-grade browser it...

  • By
    Adding Events to Adding Events in MooTools

    Note: This post has been updated. One of my huge web peeves is when an element has click events attached to it but the element doesn't sport the "pointer" cursor. I mean how the hell is the user supposed to know they can/should click on...

Discussion

  1. Cool little snippet here mate. Would mate a nice little class if it was taken further. You are part of the mooDev team arnt you? Isn’t there a spinner class in the new more version to be released soon? Will that do a similar thing?

  2. Good question Si. The doc for the new feature Spinner is here : http://mootools.net/docs/more/Interface/Spinner It automates the creation of ajax spinners over DOM elements being updated (previously known as Waiter on Clientcide.com). So your class is a bit useless, no ?

  3. @Nico: not useless no, maybe superseeded tho. Still, a nice little tut like this can help someone learn mootools and if you dont want to include mootools more then you can use it for your own purposes.

    I’m excited about trying out the spinner class tho and the table sort class (as i had just wrote my own)

  4. @Si: not to self, learn to type URLs properly!!

  5. great!!!!thanks man

  6. I think that AjaxLoader.js may be also useful to create simple AJAX spinners.
    http://musicvano.github.io/ajaxloader/
    This JavaScript library is easy to use, small and allows to modify properties of the spinner in real time.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!