Posts categorized “jQuery”.

Hide the Fancy Force Login Prompt

It really annoys me that Fancy force you to login before you can see that page you’ve click the link to view.

Here is a bookmarklet you can drag to your bookmarks bar. Then when they try to force you to log in, click it to hide the login prompt and view the page as normal.

Drag the following to your bookmark bar:

Extending jQuery selectors and understanding the options

There are tons of posts around which bang on about how to extend jQuery to include your own selectors, but to find one that actually explains your options is a pain, hence this post.

jQuery has an extend function baked in which looks something like $.extend(target, object1, object2); and will extend target (which should be an object) with the properties and methods of object1 and object2. You can add as many additional objects (think object3, object4 and object5) to the end as you like.

What we want to do it extend a very specific part of jQuery itself. The $.expr[‘:’] object which stores the built in selectors and any we add.

Indeed when you console.log($.expre[‘:’]) you see a full list of the selectors available in the version of jQuery you are using:


NB: I’ve removed the ui- selectors to avoid confusion.

So now you know what not to call your selector (unless you want to replace a built in one), we can define our own…

	block: function(e,i,m,s) {
		return $(e).css('display') === 'block';

The first thing you will notice about the above is that the selector function takes up to (although may need fewer) four argument; e, i, m and s.

e is the element currently being checked.
i is the index of that elements within all elements being checked (starts at zero)
m stands for match and is additional information that we can use in our function.
s stands for stack and is all elements in the current selection or stack

so $(e) makes the current element e into a jQuery object
i allows us to check for the 3rd, 6th, 23rd thing
m contains the following:

[0] is the actual selector called. For example :anyof(“div,4,hr,10″)
[1] is just the selector name. For example: anyof
[2] is the type of quotes used (if any) in the selector. So ” would be returned for :anyof(“div,4,hr,10”)
[3] is any parameters used in the form of a string so “div,4,hr,10” from above

and s contains an array of one or more html elements.

So now we know what is actually returned we can write a cleverer selector…

	anyof: function(e,i,m,s) {
					// correct index?
					if(parseInt(e2) == i)
						return true;
					// match selector
						return true;
			return false;
			return false;

or if we wanted to check for the unique items in the stack:

	isunique: function(e,i,m,s) {
			if($(e2).prop('tagName') == $(e).prop('tagName'))
				return false;
		return true;

NB: Since jQuery 1.6 you should use .prop() for tagName not attr()

The above selector will check all other elements in the stack and return only those with a unique tag. We could expand it to check other things about the element to see if it is truly unique such as it’s classes or styles.

NB: I haven’t checked all the code above and certainly haven’t optimised it, but it is likely to work as is or with a small number of fixes.

jQuery ajax – headers are better than the success callback

If you’ve used jQuery for even a short period of time you will have probably come across the $.ajax method and several of it’s shortcut methods like $.getJSON or $.load

Within these methods there is usually a ‘success’ callback which fires when a 200 response is received from the server. 400 and 500 responses tend to trigger the ‘error’ callback.

So success and error are pretty generic all encompassing monsters who work for many tasks, but sometimes you need to be a little bit more clever about it.

Continue Reading… »

A jQuery plugin for the masses

Every wonder how to hit the ground running with a jQuery plugin? Here is the absolute complete basics for you (and me) to copy and paste…

First we need to define our Class. I’ll call mine Boom.

var Boom = function(element, options){
	this.init('boom', element, options);

So now we have something to focus our plugin at we can add a prototype method to it. This makes the ‘new Boom’ code super slick and gives us a better control over the ‘this’ variable because we can whack it inside a function…

Boom.prototype = {
	init:function(type, element, options)
		// do stuff on create
		// makes it available almost anywhere as this.$element
		this.$element = $(element);
		// then maybe do something with the options
		// do some other clever stuff.

And finally we need to connect it up to the jQuery bit…

$.fn.boom = function (options) {
	// return this makes it chainable
	return this.each(function ()
		new Boom(this, options);
$.fn.boom.Constructor = Boom;

Very basic, but worth having here for future use. You can now call it like so…


Animating Fade Of Twitter Bootstrap Buttons

I came across a curious issue today. You can’t select a period of time over which to fade out and fade in Twitter Bootstrap buttons.

If you apply jQuery fadeTo() or animate() they fade out and they fade in, but they do it in their own time.

This may be related to a css transition which is applied in the bootstrap css, I haven’t had a chance to look, but the workaround for me is to put the button in a span and fade the span in and out instead.

   <span><a href="#" class="btn small">Button Text</a></span>