Jeremy Huang Jeremy Huang - 5 months ago 56
Javascript Question

document.execCommand('bold',false,true) does not work with jquery on("click", function(){})

I am trying to do rich text editing with javascript and jquery.

I want to create a button to embolden selected text in a content editable div. When I tried with the following code, I get the expected result.

<div contenteditable="true" style="height:100px;width:100px;border:1px solid; " class="editor"></div>
<button class='bold' onclick="document.execCommand('bold',false,true);">B</button>


However, when I used jquery to do the same thing, it was not working.

<div contenteditable="true" style="height:100px;width:100px;border:1px solid; " class="editor"></div>
<button class='bold'>B</button>


$(function(){
$("button.class").on("click",function(){
document.execCommand('bold',false,true);
})
})


Why isn't jquery working for my problem ?

Answer
$("button.class").on("click",function(){

Change this to

$("button.bold").on("click",function(){
Comments