Jump to content

Toggle Classes for Buttons in Rich Text Editor


N1CK3RS0N

Recommended Posts

I got my WYSIWYG text editor working pretty good. I'm trying to figure out how I should make the buttons classes toggle.

 

Heres the demo.

 

http://www.cigarcompendium.com/WYSIWYG/index.html

 

I want it so when bold is currently enabled on the text you are typing then the bold button class will change to active. Just not sure how to check if that effect is currently enabled in the editor.

Link to comment
Share on other sites

You are first going to have to name the items in the toolbar and change the javascript function button up a lil

so for bold do

<div onclick="button('bold')" class="button" id="boldBtn"><img title="Bold" alt="Bold" src="images/bold.png" class="image"></div>

 

function button(a) {
if(!document.getElementById(a+'Btn').style.backgroundColor) { document.getElementById(a+'Btn').style.backgroundColor = "#D5DDE5"; }else{ document.getElementById(a+'Btn').style.backgroundColor = null; }
document.getElementById('editor').contentWindow.document.execCommand(a, false, null);
document.getElementById('editor').contentWindow.focus();
} 

 

that should work for you.

 

Good luck! Have fun

Link to comment
Share on other sites

You are first going to have to name the items in the toolbar and change the javascript function button up a lil

so for bold do

<div onclick="button('bold')" class="button" id="boldBtn"><img title="Bold" alt="Bold" src="images/bold.png" class="image"></div>

 

function button(a) {
if(!document.getElementById(a+'Btn').style.backgroundColor) { document.getElementById(a+'Btn').style.backgroundColor = "#D5DDE5"; }else{ document.getElementById(a+'Btn').style.backgroundColor = null; }
document.getElementById('editor').contentWindow.document.execCommand(a, false, null);
document.getElementById('editor').contentWindow.focus();
} 

 

that should work for you.

 

Good luck! Have fun

 

Exactly what I did and here is the problem.

 

You click bold, now the bold button class is shown as active. Now when you click into an area with not-bold text, it still shows as active.

 

So the real way to do this would be to find what effects are currently applied to what you are typing, and then change the class based on that.

 

Only problem is I don't know how to find what effects are applied to the current text. Find how to do that and we got ourselves an answer. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.