Notepad Web Development

Feed icon

Posts Tagged ‘Javascript’

Auto clearing text inputs

$('input[type="text"]').each(function (i, el) {
if ($(this).val() == "") return false;
$(this)
.data('defaultText', $(this).val())
.focus(function (e) { if ($(this).val() == $(this).data('defaultText')) $(this).val(''); })
.blur(function (e) { if ($(el).val() == '') $(el).val($(this).data('defaultText')); });
});

Jquery plugin class based template

(function($){
	/*
	 *		Jquery Plugin Template
	 */

	$.fn.pluginTemplate = function(options) {

		return this.each(function() {
			var el = $(this);
			if (el.data('pluginTemplate')) return; 		// Return early if already exists on this element
			var pT = new pluginTemplate(this, options);	// Create object from our class
			el.data('pluginTemplate', pT);			// store the object as data() attached to target jquery object

			pT.init();
		});
	};

	var pluginTemplate = function(el, options) {

		var obj 	 = this;		// Ref to class
		var pT	 	 = $(el);		// DOM element we are working with

		var settings = $.extend({
			// Add 	: custom,
			// vars : here
		}, options || {});

		this.init = function() {
			// Entry point

		};
	};
})(jQuery);

 

and longer version with web service request/callback methods added

(function($){
	/*
	 *		Jquery Plugin Template
	 *		with web service request/callback methods
	 */

	$.fn.pluginTemplate = function(options) {

		return this.each(function() {
			var el = $(this);
			if (el.data('pluginTemplate')) return; 		// Return early if already exists on this element
			var pT = new pluginTemplate(this, options);	// Create object from our class
			el.data('pluginTemplate', pT);			// store the object as data() attached to target jquery object

			pT.init();
		});
	};

	var pluginTemplate = function(el, options) {

		var obj 	 = this;		// Ref to class
		var pT	 	 = $(el);		// DOM element we are working with

		var settings = $.extend({
			// Add 	: custom,
			// vars : here
		}, options || {});

		this.init = function() {
			// Entry point

		};

		this.serviceRequest = function() {
			var s = document.createElement('script');
			s.setAttribute("type","text/javascript");
			s.setAttribute("src", SERVICE_URL_HERE+'&callback=serviceCallback');
			document.getElementsByTagName("head")[0].appendChild( s );

			$('body').bind('serviceCallback', obj.serviceCallback);
		};

		this.serviceCallback = function(e, json) {
			if (!json.results) {
				pT.html('<p>Service unavailable.</p>');
				return;
			}

			$(json.results).each(function(el){

				/* deal with data */

			});
		}
	};

	/*
	 *	Global callback
	 *	Passes data to class method via custom event
	 */
	serviceCallback = function(ob) {
		$('body').trigger('serviceCallback', [ob]);
	}
})(jQuery);

Rizla Suzuki Moto GP widget

Moto GP widget created for Rizla UK.
(more…)