Skip to content
Advertisement

Onclick event on list item

I’m trying to follow this answer, but rather than using a button as he does in his jsfiddle, I’m trying to use list items:

http://jsfiddle.net/hqvDT/108/

It looks like it should work, but when I try to select some text and then press B (for bold), it doesn’t actually bold the text.

What’s wrong?

HTML:

JavaScript

CSS:

JavaScript

JS:

JavaScript

Advertisement

Answer

The problem is that when an element other than a button is clicked, the selection in the textarea disappears.

On way to fix this is to use onmousedown instead of onclick, since the mousedown event is triggered before the text selection is lost.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement