28 July 2011

IE - Using onClick on a span element to control a textarea

I created a toolbar from span elements with background-images to control the contents of a textarea below it through the onClick event. This works fine in Opera and other browsers, but of course not in Internet Explorer. It seems IE looses the selection in the textarea when the span is clicked. There are two workarounds though:

Make the span unselectable

The first one is to simply make the span tag unselectable by adding
unselectable="on"
to it.

Use the onMouseDown event

Alternatively you can also use the onMouseDown event rather than the onClick event. This way the selection won't be lost yet when your JavaScript code does it's thing. But this on of course does slightly change the behaviour, and thus I prefer the first method.

No comments:

Post a Comment