views:

119

answers:

2

I'm using a customized <img> element as a button on my site (with onclick for the form submit). The reason being I want the element to display one image when the button is up and another for when the button is down. I'm using onmousedown and onmouseup for that.

This is an AJAX-based site, and the submit is also AJAX-y. It is safe to assume that javascript is on.

Forms are being submitted by AJAX (via Prototype), so the regular <input type=button> is out of the question as it would cause a submit + page refresh (also, to my best understanding, it cannot be fully customized using images).

My question is: Should I expect any difficulties with the approach, and is there a better/easier way of generating customized buttons?

I am interested in usability and compatability issues: e.g. Accessability features (such as tab index) vs. support on all browsers (such as IE6).

+2  A: 

Use a <button> or <input type="submit"/> with CSS background styles applied.

<html>
  <head>
    <style type="text/css">
      .hoverable {
           background: #FFFFFF url(path/to/background.png) top left no-repeat;
           height: 32px; /* height and width match your background.png dimensions */
           width: 64px;
       }
      .hoverable:hover {
           background-image: url(path/to/background-hover.png);
       }
    </style>
  </head>
  <body>
    <form>
      ...
      <button type="submit" class="hoverable"></button>
      <!-- or <input type="submit" class="hoverable"/> -->
      <!-- or <button type="button" class="hoverable"></button> if you don't want submit behavior -->
    </form>
  </body>
</html>

Using a form input makes the most sense semantically, especially with your concerns about accessibility. People using accessibility tools probably aren't expecting to encounter a <div> or <img> and be expected to perform an input event on it (I could be wrong, I'm not entirely familiar with how such tools work).

The fact that the application is dynamic/ajaxy/etc. shouldn't be a barrier to you using the appropriate markup elements and using CSS to style it appropriately.

Edit:

Regarding the <input> not working: if you return false from whatever gets invoked when the button is clicked, it won't continue execution (i.e. submit the form). Example:

<button type="submit" onclick="handleClick();"></button>

...

function handleClick() {
    // ajax call
    return false;
}

On top of that, using a <button type="button"></button> shouldn't even submit the form at all. Some browsers default the type to "submit", so you'd want to explicitly define type="button" to make sure it's not treated as a submit.

Obviously, this will be different than your prototype code, but you get the picture; the gist of it is that the event handler needs to return false. And <button>/<input> can be styled just as well as an <img> or <div>.

Rob Hruska
Why is this better? Isn't `button` unsupported in IE6?
shmichael
Who cares about IE6?
Rocket
You didn't impose any browser requirements in your original question.
Rob Hruska
According to the latest W3C statistics, a population corresponding to 6.7% of internet traffic.
shmichael
@shmichael: 'W3C statistics': how did they collect them? If they did that on their own site, it's very likely that it doesn't correspond with normal users; W3's sites are usually only visited by web developers, who tend to use other browsers than IE.
Marcel Korpel
@Rob corrected the question. It would still be nice to have an explanation of why the `<button>` is better than an `<img>` or `<div>`.
shmichael
@shmichael: Your approach doesn't degrade nicely when JavaScript is turned off/unavailable.
Marcel Korpel
@Marcel that would only prove to strengthen my point. Anyway, Wikipedia's approximation for 2009 gives IE6 a 27.4% share. Either way, it can't be ignored.
shmichael
@shmichael - Semantically speaking, `<button>` (or `<input type="submit">`) make more sense. Buttons are for clicking, images and divs aren't really intended for taking user input. Using buttons helps when the page degrades, whether that be with older browsers, text-based browsers, and/or when javascript is disabled. If `<button>` doesn't work, you can just use a submit input.
Rob Hruska
@Rob - it's an AJAX submit (a totally different page is displayed without javascript - question edited). An `<input>` wouldn't do the trick. Is there any other way of degrading gracefully?
shmichael
@shmichael - Is there anything else you're not telling us here about your application that we need to know? It's tough to give an answer when the conditions keep changing.
Rob Hruska
@Rob - I can tell you all about my application, but 90% of it will probably not be relevant. In short, it is a Rails app that looks and feels a lot like Facebook.
shmichael
If you're interested, I can provide a longer overview, just give me hint of what type of info could be useful/restrictive for the question.
shmichael
@shmichael - Can you elaborate on why "an `<input>` wouldn't do the trick"? That's a somewhat dubious statement.
Rob Hruska
@Rob - Appreciate the help. Updated the question to explain `<input>`.
shmichael
A: 

You can improve these with CSS sprites, here's a good article explaining it: http://www.jaisenmathai.com/blog/2008/04/03/extremely-efficient-image-rollovers-using-css-sprites-and-no-javascript/

It's a css-only solution that uses 1 image for both the up & down states.

JKirchartz
Thanks - It's on the to-do list! However the main focus of the question is on usability and compatability issues.
shmichael