Detecting keypresses cross-browser using jquery

Date: 26th February 2009 at 8:36 pm | Filed under: development, scripts | Author: Sam Burdge | Tags: , , ,

Using keypresses as an alternate way to to navigate through websites  is a great way to make sites more accessible. For instance pressing 'p' takes the user to the previous page, while pressing 'n' takes them to the next. The benefits of using jQuery to achieve this are:

  1. Concise code
  2. Easy to implement various functions on keypress
  3. Works on IE, Firefox & Safari

DETECTING THE KEY CODE

To detect which key a user has pressed you need to set up a function that returns an ascii code each time a key is pressed. A full table of ascii codes is available here. The tricky part to this is that Mozilla and Safari define the value as charCode, while IE defines  it as keyCode. The following script is based on the 'n' key and 'p' key, it takes the links from the 'Previous' and 'Next' buttons on your site and changes page when the key is pressed. Also note that it is important to detect for both the lower-case and upper-case letter as the ascii codes will be different for each.

SOURCE

//process this function whenever a key is pressed
$(document).keypress(function (e) {
//Next
//if the keyCode or charCode is 110 (lower case n) or 78 (upper case N)
if (e.keyCode == 110 || e.charCode == 110 || e.keyCode == 78 || e.charCode == 78){
//grab the href attribute from the link with the id "next_link"
var next_link = $("a#next_link").attr("href");
//if the next link exists (i.e if it's not the last page)
if(next_link){
//go to the next page
window.location = next_link;
}
}
//Previous
//works exactly as the above example but for the 'p' and 'P' keys
if (e.keyCode == 112 || e.charCode == 112 || e.keyCode == 80 || e.charCode == 80){
var prev_link = $("a#prev_link").attr("href");
if(prev_link){
window.location = prev_link;
}
}

});

You must have the jQuery library installed on your site to run this script. For more info about jquery visit the site: http://jquery.com/

7 Responses to “Detecting keypresses cross-browser using jquery”

  • Comment by Steve Holden
    Date: June 26th, 2009 at 4:03 pm

    What happens when the page contains a form and the user types into the form inputs?

  • Comment by Sam Burdge
    Date: June 26th, 2009 at 4:27 pm

    Hi Steve
    Good question. I hadn’t tested that.
    You could always try working around this by using the focus() and blur() events for the form input to switch the keypress function on or off.

    Basic example:

    $(’input’).focus(function(){
    var keypress_active = false;
    });
    $(’input’).blur(function(){
    var keypress_active = true;
    });

  • Comment by lionel
    Date: October 8th, 2009 at 9:39 pm

    Hi, thanks for the infos, the code is working well, but is it possible to replace the windows.location by an ajax call ? thanks ?

  • Comment by lionel
    Date: October 8th, 2009 at 9:40 pm

    if yes how ? ;) thanks

  • Comment by links for 2010-03-13 | Folks Pants
    Date: April 8th, 2010 at 5:22 pm

    [...] Detecting keypresses cross-browser using jquery | Sam Burdge (tags: jquery keypress) [...]

  • Comment by Jason Cramer
    Date: March 15th, 2011 at 11:58 pm

    BRILLIANT! This was exactly what I had been looking for. Love the color scheme to your site too. Very retro DOS-ish.

  • Comment by Rachel Nabors
    Date: March 17th, 2011 at 1:49 pm

    Hello Sam! This is exactly what I am looking for for arrow-key navigation through my comics site. I need to disable it when people are typing in comment fields, though, and I just can’t seem to implement your example code for disabling above.

    Could you expand on it a bit? It would be a great help if you could!

Leave a Comment

A Single html Form With Multiple Submit Buttons - Changing a Form’s Action Attribute using jQuery

Date: 15th February 2009 at 4:48 pm | Filed under: development, scripts | Author: Sam Burdge | Tags: , , , , , ,

I was recently confronted with the problem of having a single html form with three different submit buttons, each button submitting the form data to a different php script. This is obviously impossible with html alone, as a form can only have one 'action' attribute, which dictates the URL of the file that the form's data is submitted to.

I searched around on the web for an easy way to implement this and found various examples. One of the best examples I found was on CoderLab's blog: Multiple submit buttons on a multiple blog which uses javascript to apply a different action attribute to the form depending on which button is pressed. Each button in the form has it's own onClick function like so:

<form name="myForm" id="myForm">
Search: <input type="text" id="wrdSearch"/>
<input type="button" name="google" id="google" value="Google" onClick="SendTo(this.id)"/>
<input type="button" name="msn" id="msn" value="MSN" onClick="SendTo(this.id)"/>
<input type="button" name="yahoo" id="yahoo" value="Yahoo" onClick="SendTo(this.id)"/>
</form>

(Visit the page to see the javascript that goes with it: http://blog.coderlab.us/2005/10/04/multiple-submit-buttons-in-a-form/)

As I already had jQuery installed on the site, and the CoderLab script would still need some modification to serve my specific purpose, I decided to see if I could find a simpler way to achieve a similar result using jQuery. The form on my site was a lot more complex, with many fields etc. So what I really needed was a script that would submit all the form data to each of the 3 php scripts depending on which submit button was clicked.

Firstly, I stripped the onClick functions out of the form and removed the name attributes from the buttons too, like so:

<form id="myForm" method="post">
Search: <input type="text" name="search"/>
<input type="button" id="button1" value="Submit to script 1" />
<input type="button" id="button2" value="Submit to script 2" />
<input type="button" id="button3" value="Submit to script 3" />
</form>

I then wrote the following jQuery script to change the form's action attribute accordingly and submit it when each button is pressed:

$(document).ready(function(){

$("#button1").click(function(){
$('form#myForm').attr({action: "script_1.php"});
$('form#myForm').submit();
});

$("#button2").click(function(){
$('form#myForm').attr({action: "script_2.php"});
$('form#myForm').submit();
});

$("#button3").click(function(){
$('form#myForm').attr({action: "script_3.php"});
$('form#myForm').submit();
});

});

The script should work on any browser that is compatible with jQuery (see here: http://docs.jquery.com/Browser_Compatibility)

For more information on installing and using jQuery visit the website: http://jquery.com/

10 Responses to “A Single html Form With Multiple Submit Buttons - Changing a Form’s Action Attribute using jQuery”

  • Comment by A Single html Form With Multiple Submit Buttons - Changing a Form’s Action Attribute using jQuery - PHP-update.co.uk
    Date: March 11th, 2009 at 1:55 pm

    [...] More here: A Single html Form With Multiple Submit Buttons - Changing a Form’s Action Attribute controlling j… [...]

  • Comment by Dickram
    Date: March 16th, 2009 at 6:33 pm

    You rock man. I was just looking for this and found your site. I’m using jquery too, with code igniter.
    Thank you very much!!

  • Comment by David
    Date: September 24th, 2010 at 1:04 am

    Hey, your article gives me a big help. Why using $(’form#myForm’) rather than $(’#myform’) though?? thank you very much

  • Comment by Sam Burdge
    Date: September 24th, 2010 at 1:17 am

    Hi David
    Glad it helped ;)

    there is no difference between using $(’form#myForm’) or $(’#myform’)

    i just wrote it that way as i thought it would make it clearer for the tutorial.

  • Comment by David
    Date: September 24th, 2010 at 4:25 am

    Thank you for your reply!~many thanks mate

  • Comment by Ben
    Date: October 4th, 2010 at 10:12 am

    Brilliant, cheers mate. Really helped me out of a whole

  • Comment by Ben
    Date: October 4th, 2010 at 10:13 am

    that should have said a whole world of frustration!!

    (pressed enter a little early)

  • Comment by Tom Lackey
    Date: April 27th, 2011 at 9:21 pm

    this served me well, thanks!

  • Comment by Vishal
    Date: June 10th, 2011 at 9:50 am

    Wow….great stuff… really helped a lot.. thanx buddy.

  • Comment by Mr. Serra
    Date: June 17th, 2011 at 2:57 pm

    God save the internet and people like you!
    Thank you very much!!

Leave a Comment

WP Background Tile Plugin

Date: 3rd February 2009 at 12:47 am | Filed under: plugins, wordpress | Author: Sam Burdge | Tags: , , , ,

The WP Background Tile Plugin for WordPress applies a tiled background image to your blog.  It is as simple as this:

  • Create a background image
  • Upload it
  • Apply it as the background via the WP Background Tile options page

Applying a repeat pattern to the background of a webpage is easily done using css:

body {background-image: url(example.jpg);}

However, if you're not into writing code, or don't know where to insert the css into your wordpress theme, then you will find this plugin much easier. It can also be an easy way of testing out lots of background images before deciding on one you are happy with.

INSTALLATION:

  1. Download the plugin from the link at the bottom of this page
  2. Upload the wp-background-tile folder to your wp-content/plugins folder.
  3. Activate the plugin from the Plugins page in WordPress.

USAGE:

  1. Upload your background image via 'Media => Add New' in Wordpress (or via FTP)
  2. Go to 'Settings => WP Background Tile' in WordPress
  3. Paste or type in the URL of your background image (explained in more detail below)
  4. Press 'Submit'

CREATING A BACKGROUND TILE:

A background tile is an image which is repeated in both directions over the background of a webpage. Look closely at the background of my site and you will notice the green background is not a matte colour, but a subtle texture. Right click on the background of my site and choose 'View Background Image' (Firefox) or 'Save Background As' (IE) and have a look at the image. You will notice that it is small (16 x 16 pixels), as you don't need much of the pattern to make it tile. Stripes and other geometric patterns can work well for a subtle effect.

Tip: Check out the free online Stripe Generator tool, it's a really easy way to create striped background tiles.

Background tiles can also be used for larger images such as a logo repeat. An example of this is www.mrdupes.com. To achieve this type of result using photoshop the 'offset' filter can be very useful.

GETTING THE URL OF A FILE YOU HAVE UPLOADED WITH WORDPRESS:

  1. Go to 'Posts => Add New'
  2. Click on the 'Add Media' button (a little star type icon next to 'Upload/Insert')
  3. Click on the 'Media Library' tab
  4. Click on the word 'Show' next to the file you want the url for
  5. Under 'Link URL' click on the button 'File URL'
  6. The file url should now be in the 'Link URL' text box so you can copy it.

DOWNLOAD PLUGIN:


17 Responses to “WP Background Tile Plugin”

  • Comment by pressitfor.me
    Date: February 3rd, 2009 at 4:25 am

    WP Background Tile Plugin | Sam Burdge…

    The WP Background Tile Plugin for WordPress applies a tiled background image to your blog. It is as simple as this:

    * Create a background image
    * Upload it
    * Apply it as the background via the WP Background Tile options page

    Get the plug…

  • Comment by baron
    Date: February 3rd, 2009 at 4:57 pm

    hi. Thanks for plugin

    perfect.

    Regards

  • Comment by WordPress Plugin Releases for 02/04 | Weblog Tools Collection
    Date: February 4th, 2009 at 3:18 pm

    [...] WP Background Tile [...]

  • Comment by Satranç
    Date: February 4th, 2009 at 6:32 pm

    Hi,
    This is a nice idea. Thanks for plugin.
    But I want to ask a question.
    Why did you create a table? Can we save tha background image as an option?
    Thanks.

  • Comment by WordPress Plugin Releases for 02/04 | MBConsulting.se
    Date: February 11th, 2009 at 7:17 pm

    [...] WP Background Tile [...]

  • Comment by beauty blog
    Date: February 19th, 2009 at 2:14 pm

    can you make a plugin like this to show post in a magazine like layout.

    http://perishablepress.com/press/2008/08/04/two-column-horizontal-sequence-wordpress-post-order/

  • Comment by Sam Burdge
    Date: February 19th, 2009 at 2:31 pm

    Hi beauty blog,

    I have a plugin which does exactly that! Here’s the link:

    http://www.samburdge.co.uk/wordpress/wp-post-columns-plugin-2

  • Comment by dogday
    Date: March 4th, 2009 at 6:19 pm

    I have this nice plugin running in my blog, but it isnt XHTML compliant, I think because of the ‘&’ signs used to pass varibles.

    Could you replace ‘&’ signs with ‘&’ to correct this?.

    I have tried, but my knoledge about php isnt enought to find where is called the function.

    Thanks in advance.

  • Comment by Sam Burdge
    Date: March 5th, 2009 at 2:03 pm

    Hi dogday
    The plugin doesn’t use any & symbol, this error must be coming from your theme! Also, if your theme already uses a background image then the plugin may not work.
    Sam

  • Comment by technofunk
    Date: March 12th, 2009 at 12:33 am

    great plugin.

    one question; what if i dont want this tiled? just a single image. non repeating.

  • Comment by Charles
    Date: September 21st, 2009 at 12:15 am

    Hi,

    I tried it on my site (see comment header) and it tiled only the upper half of the page. I have WP 2.7.1. on that particular site.

    Is this a bug or am I doing something wrong?

    Thanks,
    Charles

  • Comment by KiraSherrine
    Date: October 23rd, 2009 at 6:24 pm

    Hi,

    what part would you input the tile file?

    Also my nav tags (about, contact, …)
    and Sidebar Tags (category, archives)
    are all jpegs … I am looking for ways to input it into the php but cannot find any help on that…any help?

    Kira

  • Comment by bpositive
    Date: November 9th, 2009 at 1:32 pm

    Hi. Great Plugin :-)

    How do i get it to center a larger picture?

    Thanks

  • Comment by Jack
    Date: March 18th, 2010 at 5:11 pm

    Great plugin! I’m using it on two sites and it works like a champ! But on ONE of the sites, the background won’t display in IE8, while it works just fine on the other. Any ideas?
    (www.pdrjunction.com and http://www.hailreporter.com)

  • Comment by monk
    Date: May 4th, 2010 at 7:51 am

    hello

    nice plug in. i use over there ( http://www.monk.gr) and its work great.

    thanks

  • Comment by Carrie Burns
    Date: June 7th, 2010 at 4:19 pm

    Great plugin, but appears that Firefix/Netscape doesn’t like the background as a body style tag, while it works fine in Safari, hoping we can get it to work though.

  • Comment by echav
    Date: August 19th, 2010 at 3:53 pm

    Do you have an option for content background and not body background?
    Thanks

Leave a Comment

Image mouseovers without javascript - CSS & Sprites

Date: 3rd March 2008 at 1:27 am | Filed under: development, scripts | Author: Sam Burdge

This article is an extension / improvement upon another article I published on my site previously: {{post id="simple-javascript-for-image-mouseovers" text="Simple Javascript for Image Mouseovers"}}. As I am always looking for improved methods, I was discussing the pitfalls of the javascript method with a friend (and occasional comment writer / contributor to my site) who pointed me in the direction of CSS and image sprites. This lead me to research the topic further, and I found two articles in particular which were incredibly useful:

Read on…

5 Responses to “Image mouseovers without javascript - CSS & Sprites”

  • Comment by Jonn
    Date: April 29th, 2008 at 1:15 pm

    Very nice article. Thanks.

  • Comment by hot girls publisher
    Date: July 13th, 2008 at 7:20 am

    I used this article in the making of this website! Thanks! http://www.girlsbeinghot.com

  • Comment by Ray
    Date: February 18th, 2009 at 4:36 pm

    Super-informative site! I am loving it!! Will come back again - taking you feeds also, Thanks.

  • Comment by Иван Евсеев
    Date: June 13th, 2009 at 4:14 am

    Замечательно написано, но как говорится, для полноты информации нужно минимум хотя бы три источника :)

  • Comment by Datsu
    Date: July 31st, 2009 at 8:20 pm

    This was extremely helpful; exactly what I was looking for and simple to implement. Thank you!

Leave a Comment

WordPress - Change Default Email Address Plugin

Date: 20th February 2008 at 12:04 am | Filed under: development, plugins, wordpress | Author: Sam Burdge

This WordPress plugin changes the default email address that all notifications sent from your blog are addressed from. The default address for all emails sent by your blog is currently wordpress@ yoursite.com.

I had seen some tutorials / forum entries previously where people suggested editing the core WordPress file 'pluggable.php' to overide this default. This method does work, however you would have to repeat the hack every time you upgrade to a new version of WordPress so it is not an ideal solution.

Faced with this problem for a site I was working on recently, I wrote this basic plugin which allows you to configure your own email address in the format: Your Name <yourname@yoursite.com>.

Installation:

  1. Upload the file wp_change_default_email.php to your wp-content/plugins folder
  2. Activate the plugin from the Plugins page in WordPress

Usage:

  1. In the Plugins page in WordPress click the edit button next to the plugin in the list.
  2. Scroll down until you see a note saying 'Configure it'
  3. Enter your name and email as shown
  4. If you don't enter a new address the default 'wordpress@' will remain

Download:


40 Responses to “WordPress - Change Default Email Address Plugin”

  • Comment by Shane
    Date: February 29th, 2008 at 8:33 am

    Works great - thank you!

  • Comment by Change Default Email Address » Wordpress Plugins
    Date: July 9th, 2008 at 6:56 am

    [...] Version: 0.1 - License: n/a - Author: Sam Burge - Plugin Page - » Download [...]

  • Comment by Shane
    Date: August 25th, 2008 at 10:27 pm

    Is there a newer version than 0.1? I upgraded to WordPress 2.6.1 and the Plugins area is telling me that there is a new version, but the new version links to:
    GALLERY LIGHTBOX PLUGIN
    http://wordpress.org/extend/plugins/wp-25-gallery-lightbox-plugin/

    Also, just an FYI, version 0.1 now conflicts with the Subscribe2 plugin.

  • Comment by Greg
    Date: September 17th, 2008 at 8:06 pm

    Very handy little plug-in, works like a charm, thanks.

    I’m experiencing a little weirdness, though. I downloaded it from your site. When I install it, it works fine but I get an alert telling me there’s a new version 1.3 (the code says “Version 0.1″) and the link to get the new version points at http://wordpress.org/extend/plugins/wp-25-gallery-lightbox-plugin/ which is clearly wrong. The “Upgrade automatically” option doesn’t work either, with the error mentioning wp-25-gallery.

    Since it works, the only reason I care is I don’t want the little orange flag over “Plugins” in my dashboard, but seems like something you’d want to know.

  • Comment by huski
    Date: October 23rd, 2008 at 10:19 am

    wordpress 2.5
    not working for me, i made the two changes , name and email to the php file, verified the changes were saved in the php file, but it continues to send wordpress and wordpress@domain

    ????

  • Comment by m1k3y
    Date: February 9th, 2009 at 11:06 pm

    Great Job!
    THX !!!

  • Comment by marco
    Date: February 16th, 2009 at 5:08 am

    Hi

    IS the email address restricted to the domain of the website? OR can any email address be used, say gmail account?

  • Comment by marco
    Date: February 21st, 2009 at 11:50 pm

    Hi

    This appears like it will do the (magik) trick but I have an unusual situation in which the email address my client wants for default does not use the website’s domain name in it. So simply setting the name in front of the @ symbol is not sufficient - and neither is my confidence in editing your php to accomodate it. Could write up a note on how this customization in the php file would go?
    Sorry for the xtra trouble. Really appreciate your efforts with this as you can imagine.

    /marco

  • Comment by Açıklamalı 200 Wordpress Eklentisi (Part2) | Paylaşım Keyfi
    Date: March 1st, 2009 at 7:25 pm

    [...] © Change Default Email Address Wordpress varsayılan e-posta adresini değiştirebilmenizi [...]

  • Comment by Beth
    Date: August 3rd, 2009 at 6:40 pm

    I had no idea this was even an issue. I thought that it would just show up with my email by default. That’s dumb.

  • Comment by WordPress için tam 400 eklenti birsayfada! | PiyanoDegil.org :)
    Date: September 2nd, 2009 at 2:59 pm

    [...] © Change Default Email Address Wordpress varsayılan e-posta adresini değiştirebilmenizi [...]

  • Comment by Marcus Naylor
    Date: September 8th, 2009 at 3:35 pm

    Made the changes in the plugin editor, added my From Name and the from Address, updated changes and nothing happened. Still coming from Wordpress

  • Comment by Açıklamalı 200 Adet Wordpress Eklenti Arşivi 2 | Akgün-Web Ailesi
    Date: September 18th, 2009 at 5:29 pm

    [...] © Change Default Email Address Wordpress varsayılan e-posta adresini değiştirebilmenizi [...]

  • Comment by Como mudar endereço email do Wordpress
    Date: October 30th, 2009 at 10:49 am

    [...] ou pode fazer Download do plugin AQUI [...]

  • Comment by Adam
    Date: November 3rd, 2009 at 5:05 pm

    This is a life saver. We were testing the site and discovered the “wordpress@”. We knew we didn’t want that. Wordpress really need to allow users to change this from the dashboard. But anyway, this works perfectly and I wanted to thank you Sam for sharing your expertise with us.

  • Comment by Harry
    Date: November 6th, 2009 at 10:40 am

    Thanks for the plugin.

  • Comment by Tony
    Date: November 14th, 2009 at 1:27 pm

    Works Great!!!!! Thanks!!!

  • Comment by Rhino
    Date: November 21st, 2009 at 12:36 pm

    Hi,
    Thanks for your great plugin. I were used it in my blog, it run so cool. But i have a problem with my language. I use Vietnamese language and the email appear: [Tranh biếm hỠa Việt Nam] Có thà nh viên mới đăng kí

    Can you help me this problem?
    Thanks!

  • Comment by Wordpress 400 Eklenti « www.hakanersoy.org
    Date: January 2nd, 2010 at 12:53 pm

    [...] © Change Default Email Address Wordpress varsayılan e-posta adresini değiştirebilmenizi [...]

  • Comment by Norguad
    Date: January 8th, 2010 at 7:04 pm

    Hi, I used the plugin, but it turns all non-latin characters into weird characeters (for example: é is é or ř is Å™). Do you know how to solve it?

    Thank you very much.

  • Comment by Heath
    Date: March 9th, 2010 at 2:32 am

    Great
    thanhk you sam

  • Comment by Peter F.
    Date: March 9th, 2010 at 3:53 am

    Awesome plugin, guys! Works like a charm, and solves a problem I was afraid would be very tedious.

  • Comment by sol97one
    Date: March 10th, 2010 at 7:03 am

    Thanks a lot ! Very useful !

  • Comment by Terje
    Date: March 15th, 2010 at 12:51 pm

    Same as the others, i have problem with some signs. I try to write the norwegian letter “ø/ø” (ascii ø), but i cant get it to work

  • Comment by frank
    Date: March 21st, 2010 at 12:08 pm

    Hi there, this is a very helpful plugin, I have the same problems with the charset or something else. After installing the plugin I always receive weird characters in my mails. ÖÄÜ are not transformed properly. Thanks. F

  • Comment by Mike
    Date: April 5th, 2010 at 2:03 pm

    Great, thanks for the plugin…If you all are looking for a less extensible…but easier way to modify the email “From” line in wordpress, it’s on line 352 in pluggable.php. Just change [code]$from_name = ‘WordPress’;[/code] to something like…[code]$from_name = ‘Mike’;[/code]

  • Comment by Joshua
    Date: April 5th, 2010 at 11:52 pm

    I am using WP MU 2.9.2. I have installed the plugin and activated successfully. Yet I dont see any settings to configure !!!??

  • Comment by me2t
    Date: April 18th, 2010 at 2:02 pm

    i love this plug in very much.thanx

  • Comment by bosschan
    Date: May 9th, 2010 at 2:16 pm

    give thanx!

  • Comment by Jim
    Date: May 20th, 2010 at 2:10 pm

    Hey, thanks for this plugin, helped me sort out a really annoying reg problem I had :D

  • Comment by PLnet
    Date: May 25th, 2010 at 3:32 pm

    thank y

  • Comment by Greg
    Date: June 27th, 2010 at 2:25 am

    Great plug-in, just what I was looking for as I didn’t want any emails say “wordpress”

  • Comment by Thiago Dias
    Date: August 12th, 2010 at 3:45 am

    thanx!

  • Comment by TomCap
    Date: September 6th, 2010 at 1:56 am

    Worked like a charm. Another little irritating item solved. Thanks!

  • Comment by Maira
    Date: January 21st, 2011 at 12:59 am

    worked. highly recommend. thank you!

  • Comment by oriflon
    Date: February 17th, 2011 at 11:49 pm

    how to edit manual?

  • Comment by SteveB
    Date: February 18th, 2011 at 12:20 am

    I loaded the plug in but can’t find it in the plugin list.

  • Comment by haohuu_lam_em
    Date: March 3rd, 2011 at 10:04 am

    Can not convert to UTF-8!

  • Comment by Demo Games
    Date: April 8th, 2011 at 9:20 am

    Thanks for sharing works fine on wordpress 3.1.1

  • Comment by I Shoot Reno
    Date: April 19th, 2011 at 9:23 pm

    Fantastic solution! Minimal effort needed to make this work from a novice’s standpoint!

Leave a Comment