autoPopulate 0.1

I’ve had it with filling in the same details repeatedly while testing a new form’s validation!

So much so, that I’ve spent time writing a pointless bookmarklet that makes me even more lazy and probably isn’t doing anything for my typos to real words ratio, however, I thought it might be useful to other people too.

There’s a demo here, and a generator for your own custom one here too.

The uncompressed source is linked at the bottom of the demo page, in case you want to see how it works or extend it. There are some comments there too. (But not many…)

Minify CSS – Shrink your CSS down to size

I know there are probably many services offering it already out there, and it’s not too hard to run right in TextMate, but I haven’t been able to find it in 30 seconds of Googling (well, I couldn’t at work, but I’ve since found this… Oh well!), so I set up a quick script to do it for me:

http://dom.hastin.gs/files/minifycss/

If it’s useful, I might set up a memorable URL, let me know in the comments 🙂

I did set up syntax highlighting, using codemirror, which looked pretty nice, but made the minification (on large-ish files) take far too long. I haven’t really tested on any huge files, so try this at your own risk, but it seems faster that TextMate was.

jQuery – Persistent Input Labels

Do you forget whats supposed to go in the field the second you tab onto it?

Are you concerned your users will too?

Never fear! This simple plugin places an element of the same type underneath the current element and shows the contents of the title attribute (or whatever attribute you decide to use) as a label until you type text into the box!

It’s a very early version, and probably has a few bugs, but I’ve implemented enough to make it fairly useful I think. Unfortunately when you resize a textarea in webkit, it doesn’r fire the resize event, so I’ve added a workaround using mousemove (I tried to only capture mousemove if a mousedown had been detected, but to no avail…)

You can see a demo here, and get the files here.

Dropdownify – Minimal effort dropdown

So recently I was asked to change a navigation style of an existing site to drop-down menus.

Simple, I thought, just use one of the many existing drop-down plugins. I tried many, but most seemed to use hardcoded styles and I had a few problems (some of which I encountered again, writing this).

So I’ve made this, I think it’s fairly robust, but I’m sure there’ll be problems with embedded objects (flash) and select boxes (in <= IE6), but for my needs, it sufficed, so I thought I’d share, in case anyone else needs a simple script to manage drop-downs.

To use, create a nest of elements like this:

<ul id="top">
  <li>
    <ul>
      <li></li>
    </ul>
  </li>
</ul>

or something similar (any elements, should work) and call:

// jQuery:
$('ul#top').dropdownify();

// mootools
$('top').dropdownify();

Which should create a simple drop-down.

There are a couple of examples, one for jQuery and one for mootools, and the files can downloaded here and here.

I did encounter a problem with IE, mainly due to z-index faults, so with HTML code like the following:

<div class="nav-wrapper">
  <ul id="top">
    <li>
      <ul>
        <li></li>
      </ul>
    </li>
  </ul>
</div>

I used the following CSS:

div.nav-wrapper,
div.nav-wrapper ul,
div.nav-wrapper ul li {
  position: static;
  z-index: 100;
}

jQuery Ajaxify – Update

I’ve been doing more work on the jQuery AJAXify plugin, it’s now a lot more robust (I think…) and I’ve squashed a few bugs that were in the previous version (with help from Andrea Battaglia) along with adding in a few new features (with their own bugs no doubt!).

Theres an updated test page and as ever a direct link to download here.

Check out the test page to see the new features!

Edit: Further updates, all links have been updated to version 0.3b. Added (and documented existing) callbacks as requested by Sebioff! And a further fix for the ‘append’ option. Also fixed IE6 compatibility.

Further Update: Another update after the comment from David Lee about $(‘form’).serialize();

Another Further Update: I have set up a Google Code repository for this plugin, which states that this code is released under the MIT license.

Update October 2010: I have updated this plugin for jQuery 1.4.3 as a ‘lite’ version.

String Variable Concatenation

I stumbled across something odd today in PHP:

$r = '';
$r .= $r .= $r .= 'a';

Now, personally, I’d have expected a syntax error from the above code, but the result was even more confusing at first…

print $r; // 'aaaa'

Not sure if this was the expected output or not I tested similar code in other languages:

Ruby:

r = ''
r += r += r += 'a'
puts r # 'a'

Python:

r = ''
r += r += r += 'a'
#   File "", line 1
#     r += r += r += 'a'
#             ^
# SyntaxError: invalid syntax

Javascript:

var r = '';
r += r += r += 'a';
alert(r); // 'a'

Perl:

my $r = '';
$r .= $r .= $r .= 'a';
print $r; // 'aaaa'

That explains it!

So the reason the string is ‘aaaa’ seems to be that the code is evaluated from right to left:

$r = '';
$r += $r += $r += 'a';

// How it works:
//
// $r += 'a'; // 'a'
// $r += $r += 'a'; // 'a' + 'a';
// $r += $r += $r += 'a'; // 'aa' + ('a' + 'a')

I don’t think it’s a bug, well, at least I assume not, but is there a name for this?

Update: I asked some clever people for help understanding it.

jQuery Ajaxify

Probably not the first person to implement this, but I wanted to make an automatic form AJAX-‘ifier’ that could capture which submit button had caused the submit event to be fired too.

It’s implemented fairly simply:

$('form').ajaxify();

There’s a demo page or you can just download it and have a look.

Update: As pointed out by Andrea in the comments below, this would return the value of radio and checkboxes even if they weren’t selected. I have released 0.1a and updated the link above to fix this issue. Thanks Andrea!

Update 2: I have updated this plugin, view this post for more information.

Update October 2010: I have updated this plugin for jQuery 1.4.3 as a ‘lite’ version.

jQuery addCaptions 0.2 – Minor update

I’ve been using the jQuery addCaptions plugin and have come across a couple of new features that I felt were required and have fixed a bug in the previous verison.

I have added an exclude option to the settings so you can prevent captions being added to elements not required (like a reCaptcha box…). It’s used like this:

$('div.demo').addCaptions({
  'exclude': {
    'alt': [
      /orange/
    ]
  }
});

This would exclude any images that have the word ‘orange’ in the the alt attribute.

I also fixed a minor bug in which the script would look for captions in elements in reverse order (whoops!).

The only other change is making the elements found within the container a selector instead of an element option and a class option.

There is an updated demo here, or you can just download the script here.