Article
jQuery Ajax Loading Image
Show a centred loading image while Ajax calls run, then reveal the page when they finish.
1 min read#JavaScript#jQuery
Tag
Show a centred loading image while Ajax calls run, then reveal the page when they finish.
Populate a dropdown list from a JSON service using $.getJSON and $.each.
$.getJSON("URL", null,
function (data) {
$.each(data.d, function (i, result) {
var optionText = "<option value='" + result.value + "'>"
+ result.text + "</option>";
$('#ddlSelectTagId').append(optionText);The fastest way to empty a select element: replace it with a shallow copy of itself.
function ClearOptions(id) {
var selectObj = document.getElementById(id);
var selectParentNode = selectObj.parentNode;
var newSelectObj = selectObj.cloneNode(false); // shallow copy, no options
selectParentNode.replaceChild(newSelectObj, selectObj);
return newSelectObj;A reusable $.ajax template for calling a JSON service with success and error callbacks.
$.ajax({
url: GETURL,
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: successCallback,Split a string on a delimiter using the built-in JavaScript split function.
var split = 'john smith#5551234567'.split('#');
var name = split[0]; // john smith
var mobile = split[1]; // 5551234567Remove duplicate options from a select element with a short jQuery loop.
var usedDisplayNames = {};
$("select[name='tagname'] > option").each(function () {
if (usedDisplayNames[this.text]) {
$(this).remove();
} else {
usedDisplayNames[this.text] = this.value;