I have a spinner that displays when an ajax request is made using jquery:
$(document).bind("ajaxStart", function() {
$('#myloader').show();
});
$(document).bind("ajaxStop", function() {
$('#myloader').delay(1000).fadeOut('fast');
});
The request is actually made on keyup in this case, I’m trying to work out how to use this spinner on multiple fields, so the spinner would display next to each field.
Placing the spinner image isn’t the problem, but connecting the the image to each field is what i’m finding difficult. If that makes sense?
–UPDATE–
The answers are very helpfull but I dont think its what im looking for.
I have the following code:
<div class="form_element">
<%= f.label :email, :class=>"field_hint", :title=>"Email" %>
<%= f.text_field :email %>
<div class="myloader" style="display:none;">
<p>
validating...
</p>
</div>
</div>
<div class="form_element">
<%= f.label :username, :class=>"field_hint", :title=>"Username" %>
<%= f.text_field :username %>
<div class="myloader" style="display:none;">
<p>
validating...
</p>
</div>
</div>
Both of these fields have a keyup that does a remote call. I have changed the code slightly:
$('.form_element').ajaxStart(function() {
$(this).children('.myloader').show();
});
$('.form_element').ajaxStop(function() {
$(this).children('.myloader').delay(1000).fadeOut('fast');
});
At the moment when an the ajax happens both spinners appear, I am trying to work out how to make each spinner div relevant to its field.
I am new to Ajax so any advice on maybe doing the ajaxStart/Stop differently are more than welcome.
Were you looking for something like this?
EDIT
Here is another solution that might work for you assuming the event is always keyUp (you could also abstract this into a plugin, passing the event, in this case ‘keyup’ as an option):