Jquery
Nifty Tricks: Setting a fallback source if an image fails to load.
This useful little snippet allows you to set a backup source for an image if it fails to load. useful in a lot of situations, e.g. listing a directory contents, putting an image next to each item based on it’s extension using this code snippet if you do not have an image for a particular extension it will fall back to your default.
var backup_src = 'http://domain.ext/images/backup.png';
var wanted_src = 'http://domain.ext/images/wanted.png';
var image = $('
'); //or point to an image that is already on the page!
image.on('error', function () {
image.prop('src', backup_src);
});
image.prop('src', wanted_src);
0
#1 Crime of jQuery – DOM Abuse
The Crime
Here is an example,
$(function(){
$('#id').change(function(){
//do stuff
});
$('#id').css('border-color', '#ff0000');
var val = $('#id').val();
});
See what is happening here?
There are several calls to $(‘#id’).
The reasoning
Why is this so bad?
Well, imagine the DOM as a giant pool of all elements on your page, every time you call $(‘#id’) you are diving into the pool and swimming around until you find the element you need. That adds some serious overhead on every call!
Jquery serializePost()
jQuery has a couple of useful functions that you should be using quite a lot. .serialize() and .serializeArray().
These useful functions will take your form and spit out something you can send off in your ajax requests.
“Awesome” you exclaim! “I’ll use it to send my post“.
Not so fast!
Turns out these functions are more or less useless for post requests.
That’s where this nifty little function comes in handy. call $('form').serializePost() on your form and it will serialize your post data and return a nice little javascript object ready to send off. On the other end you can access it as if you had submitted the form normally using $_POST['myField'].
Jquery – Color Picker

It depends on jQuery 1.4.2. The Jquery powered Color Picker is styleable using a single stylesheet. It works in an unobtrusive fashion, by just turning html select inputs into a sexier equivalent. There’s no extra markup needed.

