Javascript / JQuery: Detecting the delete key

Recently I was using JQuery to detect when the delete key was pressed. I was having some issues getting it to work. I was listening to the “keypress” event, but the delete key was never detected.

$('html').keypress(function(e){
    if(e.keyCode == 46) {
        alert('Delete key pressed');
    }
});

After a bit of research I realized that “keyup” made more sense. “keypress” is for printable characters. “keydown” and “keyup” will capture all characters

$('html').keyup(function(e){
    if(e.keyCode == 46) {
        alert('Delete key pressed');
    }
});

I hope this helps someone else down the road that may be stuck on this.

JQuery 1.3 domination through deprecation

jquery 1.3

On a project I maintain, we recently rolled up to JQuery 1.3.1 from JQuery 1.2.6. Upon doing so, several things instantly broke.

I started seeing errors on lines like this:

if($('input[@name=username]').val() != "")

I decided to check the JQuery 1.3 relase notes to see what had changed.

The first bullet point answered my problem:

The ‘@’ in [@attr] has been removed. Deprecated since 1.2 this old syntax no longer works. Simply remove the @ to upgrade.

As it turns out, the @ style selector was depricated and wasn’t suggested for use when JQuery 1.2 was released, but it was still supported. At 1.3 the @ style selector was removed all together, and as a result broke our code!

Following the upgrade instruction I removed the @ symbol from the selectors, and the code started running again.

The result:

if($('input[name=username]').val() != "")

A simple and effective upgrade, and a reason to closely pay attention to release notes

The GmailThis! bookmarklet

I use GMail as my main email client. Often I’m at work and I see something online that I think is useful, and I want to remind myself of it. I have tried righting down URLS on scraps of paper in the past, but sending an email to myself works as a good reminder for me. As a bonus, I can pick up my GMail form anywhere, including my iPhone.

I’m a programmer, so I am lazy by nature. It’s kind of a pain to open a new browser, and navigate to GMail.com and then copy and paste the URL of the site, and text into GMail and then send it.

Enter the GmailThis! bookmarklet by Doug Ward. Bookmarklets are bits of javascript that you save as bookmarks in your browser. When you click that bookmark the javascript runs. Basically bookmarklets are little javascript plugins that work in almost any browser.

The GmailThis! bookmarklet will open up a new Gmail window in a little popup. The body will have the URL of the page you are currently on, and any text you happen to have highlighted at the moment. All you have to do is enter the recipients name.

You can get the GmailThis! bookmarklet from Doug Wards site, http://contrapants.org/blog/2005/07/gmailthis.html

Doug also made a cool video showing how simple the bookmarklet is to use.

If you are a heavy GMail user like I am, this is a must have.