Reputation: 10366
This works fine except I want the loading-indicator
div to show up when I select a new value from the dropdown. The first time it runs, the loading indicator appears then disappears once the remote content is loaded. I go change the dropdown menu to a different value, but the loading indicator doesn't re-appear. The "new" content eventually shows up once it finishes loading.
<select name="branch_name" id="branch_name">
<option value="Branch1">Branch1</option>
<option value="Branch2">Branch2</option>
<option value="Branch3">Branch3</option>
</select>
<div id="mycontent">
<div id="loading-indicator" style="display:none">
Scanning subversion<img src="/images/ajax-loader.gif" /></div>
</div>
<script type="text/javascript">
$(function(){
$('#branch_name').bind("change", function() {
$('#loading-indicator').show();
$('#mycontent').load('/tags/runme',{branch_name: $(this).val()});
});
});
</script>
Upvotes: 1
Views: 510
Reputation: 392
You can use .not()
to exclude your loading indicator.
$("#mycontent > div").not('#loading-indicator').hide();
Upvotes: 0
Reputation: 359836
.load()
replaces the element's HTML, so move the loading indicator outside of #mycontent
, and hide it manually.
<div id="loading-indicator" style="display:none">
Scanning subversion<img src="/images/ajax-loader.gif" />
</div>
<div id="mycontent"></div>
<script type="text/javascript">
$(function(){
var $spinner = $('#loading-indicator');
$('#branch_name').bind("change", function() {
$spinner.show();
$('#mycontent').load('/tags/runme',{branch_name: $(this).val()}, function () {
$spinner.hide();
});
});
});
</script>
Upvotes: 2
Reputation: 28906
The loading indicator is inside the div that you are loading with content. Your loaded content replaces everything in that div, including the indicator.
To fix, move the indicator outside the div:
<div id="loading-indicator" style="display:none">
<div id="mycontent">
Scanning subversion<img src="/images/ajax-loader.gif" /></div>
</div>
or .prepend()
it after loading the content.
$("#mycontent").prepend('<div id="loading-indicator">');
Upvotes: 1
Reputation: 2807
You can use the callback like this:
$('#mycontent').load('/tags/runme',{branch_name: $(this).val()}, function(data) {
//Do your stuff here, this will be executed once the request is over.
$('#loading-indicator').hide()
});
Ref: http://api.jquery.com/load/
Upvotes: 1