Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5929091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:22:33+00:00 2026-05-22T14:22:33+00:00

How can I simulate user-selection of some style from the styles-box, through JS? I

  • 0

How can I simulate user-selection of some style from the styles-box, through JS? I want to put some shortcut buttons that assign some of the popular styles with one click.

EDIT:

  • I don’t care if it’ll be in-editor button or outer button.
  • I don’t want css-style assignment; I want CKEditor-style assignment (those of the styles-box).
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:22:34+00:00Added an answer on May 22, 2026 at 2:22 pm

    I haven’t used CKEditor, but, I saw your question and thought “That would be fun to figure out.” Well, here is what I figured out:

    (yes, I found terrible documentation, but, that’s not the point…I will give them props for commenting their code, though.)

    ///
    // function to add buttons that trigger styles to be applied.
    //
    // editor - CKEDITOR - instance of editor you want command attached to.
    // buttonName - String - name of the button
    // buttonLabel - String - humane readable name of the button
    // commandName - String - name of command, the way to call this command from CKEDITOR.execCommand()
    // styleDefinition - StyleDefinition - obj defining the style you would like to apply when this command is called.
    ///
    
    var addButtonCommand = function( editor, buttonName, buttonLabel, commandName, styleDefiniton )
    {
        var style = new CKEDITOR.style( styleDefiniton );
        editor.attachStyleStateChange( style, function( state )
            {
                !editor.readOnly && editor.getCommand( commandName ).setState( state );
            });
        editor.addCommand( commandName, new CKEDITOR.styleCommand( style ) );
        editor.ui.addButton( buttonName,
            {
                label : buttonLabel,
    
                command : commandName
                //adding an icon here should display the button on the toolbar.
                //icon : "path to img",
            });
    };
    
    //Get the editor instance you want to use.  Normally the same as the ID of the textarea CKEditor binds to.
    var editor1 = CKEDITOR.instances.editor1;
    
    //If you look at ckeditor/_source/plugins/styles/default.js you will see that this selects the first element.  That list is read into the array 'default'.
    var blueTitleStyle = CKEDITOR.stylesSet.registered.default[0];
    
    //Or, you can define the style like this: See http://dev.ckeditor.com/wiki/Components/Styles for more info on style definitions.
    var blueTitleStyle = { 
        name : 'Blue Title',
        element : 'h3',
        styles : { 'color' : 'Blue' }
    };
    
    addButtonCommand(editor1, 'BlueTitle', 'BlueTitle', 'bluetitle', blueTitleStyle);
    

    Here is a Javascript function to aid your click events:

    //function used to execute the command.  Only used for calling the command when not calling from a button. (Like an A with an onClick bound to it.)
        //pulled this function right out of the api.html example in the ckeditor/_samples dir.
        function ExecuteCommand( commandName )
        {
            // Get the editor instance that we want to interact with.
            var oEditor = CKEDITOR.instances.editor1;
    
            // Check the active editing mode.
            if ( oEditor.mode == 'wysiwyg' )
            {
                // Execute the command.
                // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#execCommand
                oEditor.execCommand( commandName );
            }
            else
            {
                alert( 'You must be in WYSIWYG mode!' );
            }
        }
    

    Now, you can create a link like this:

    <a href='#' class='setBlueTitle'>Set Blue Title</a>
    

    and use a bit of jQuery to spice it up:

     <script type="text/javascript">
                $(document).ready(function(){
                    $(".setBlueTitle").onClick(function(e){
                        //stops the click from changing the page and whatever other default action would happen.
                        e.preventDefault();
    
                        ExecuteCommand('bluetitle');
                    });
                });
            </script>
    

    I am not 100% sure about the button icon part. I didn’t have an icon to try it with. But, according to a few posts, it should work fine. Regardless, the jQuery click binding works.

    That should be pretty much it! I had to do quite a bit of digging around to figure this out, but it certainly is satisfying to see it work!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code, that simulate google images effect. How can I animate nested
Does anyone know of a class/library/etc. that can simulate 32 bit unsigned integers on
How can I simulate an incoming call from a private number -- in an
Lets say I want to simulate a particle state, which can be normal (0)
I want to simulate user input to a WPF TextBox. I want to input
I've been looking around for a good web automator (something that can simulate a
Can I simulate form like behaviour when a user clicks on a simple link?
How can I simulate user input in the middle of a function called by
Can I use System.Threading to simulate user connection speed or is there a better
I have MainPage and FriendsPage.How I can simulate the back button if user clicked

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.