views:

170

answers:

5

I'm currently trying to set up a web page for a touch screen computer, and have noticed that (with my fat fingers) that onclick events are really hard to trigger, as the surface area of my finger generally causes the cursor to move while I "click".

The solution I want to use is forwarding onmousedown events to onclick. How can I make this work?

The best answer would be along the lines of:

<input type="radio" onmousedown="this.onclick()"/>

(but of course in a working condition)

EDIT: My specific example is for radio buttons - when one is checked the others are unchecked; I don't want to have to replicate this behaviour in a custom event when the browser gives it to me for free.

NOTE: Having edited, and received feedback, I think that perhaps I might be on the wrong track... perhaps it's not the "click" function on a radio button that actually does the selection: new suggestions welcome ...

+1  A: 

Why not just handle the mousedown event?

Tim Down
I'm guessing there's already code/logic implemented in the click handler that he'd also like to happen on mousedown. In other words, having a single implementation and multiple events that trigger it.
inkedmn
In which case he could abstract out that logic into a separate function which both the click and mousedown handlers call.
Tim Down
I've clarified the question... because the click function on a radio button is given by the browser... perhaps it's not the click?
Stephen
As noted in other answers, just setting the checked property of the radio button to true will do the trick.
Tim Down
A: 

Maybe this works for you

document.getElementById("yourinput").onmousedown = function() {
   this.click();
}

Additionally you could style your input buttons/fields to be a little bigger, for better accessibility via the pointer.

Alex
+1  A: 

I know you didn't mention it in the question, but if you happen to be using jQuery, it'd be really really easy:

$().live('mousedown', function() { this.click(); });
nickf
Great solution, fixes it once and for all for all mouse down events. Wrt jQuery, I'm sure the same thing could be implemented in a number of other frameworks too
jklp
Oh, I don't have enough points, but the function inside the callback should be $(this).click();
jklp
I think that `this.click()` works in plain javascript. If not, then yep, wrap the object in jQuery and it'll be fine.
nickf
In YUI3, the same is YUI().Event.simulate(this, 'click'). However, as added in my NOTE edit in the question, perhaps I'm trying the wrong thing...
Stephen
A: 

It's better to have a single function to handle both events, e.g.

var function = foo(e) {
   ...
};

<input onclick="foo();" onmousedown="foo();"/>

Better still use unobtrusive JavaScript, e.g. (if you were using YUI):

<input id="input-el"/>

var function = foo(e) {
   ...
};

var Event = YAHOO.util.Event;

Event.on('input-el', 'click', foo);
Event.on('input-el', 'mousedown', foo);
wrumsby
+1  A: 

Ok, after your edits it becomes clearer - what you want to do is to simulate a user clicking the control when the mousedown event is fired.

Two options:

  1. You can fire the DOM event - this tutorial on DOM events discusses this (see the Manually firing events section); Firing Javascript Events covers similar ground.
  2. Set onmouseout="this.checked = true;", i.e. clicking the radio button "checks"/selects the radio button.
wrumsby
From "this tutorial on DOM events": the following code works quite well (FF 3.5.1): var fireOnThis = document.getElementById('someID');var evObj = document.createEvent('MouseEvents');evObj.initEvent( 'click', true, true );fireOnThis.dispatchEvent(evObj);but has some consistency issues. "this.checked = true" seems to work every time...
Stephen
checked = true. I can't believe it never occurred to me that radio buttons don't toggle (like checkboxes). However, I do want to apply the same sort of functionality to checkboxes later...
Stephen