Change Background Color of Ajax HtmlEditorExtender using ColorPickerExtender

[Demo] Change Background Color of HtmlEditorExtender

[Demo] Change Background Color of HtmlEditorExtender


In this Blog, we will learn to change the Background Color of Ajax HtmlEditorExtender using ColorPickerExtender.

Background

There was an question, which eventually gave birth to this Blog.

Here comes the Logic

We have already discussed the Logic in one of the previous Blog Set Content inside AJAX HTMLEditor and EditorExtender using JavaScript. We just need to find out the Edit Panel, that is the div, which is actually showing data for HtmlEditorExtender. Then we can easily assign Selected Color of ColorPickerExtender to that div.

<div contenteditable="true"
     id="txtEditorDemo$HtmlEditorExtenderBehavior_ExtenderContentEditable"
     style="height: 80%; overflow: auto; clear: both;"
     class="ajax__html_editor_extender_texteditor">
</div>

Okay ! Let’s get our feet wet

Get the ColorPickerExtender Selected Color

We can easily get the Selected color by handling the OnClientColorSelectionChanged Event of ColorPickerExtender. We can call a JavaScript function on this Event. Let’s see the markup.

<asp:ColorPickerExtender runat="server" 
                         TargetControlID="txtSelectColor"
                         SampleControlID="sampleBodyColor"
                         PopupButtonID="txtSelectColor" 
                         OnClientColorSelectionChanged="colorChanged">
</asp:ColorPickerExtender>

So, the JavaScript function is colorChanged(). Following code would alert you the Selected Color.

function colorChanged(sender) {
    alert(sender.get_selectedColor());
}
Selected Color on Alert Box

Selected Color on Alert Box

Now, let’s get the HtmlEditorExtender and change its Background Color

There are two ways to do this.

  • Using Sys.Application $find Method

    First find the HTMLEditorExtenderusing ID, then assign the Selected Color to the Background Color Property of its EditableDiv.

    var htmlEditorExtender = $find("<%= heeEditorDemo.ClientID %>");
    htmlEditorExtender._editableDiv
                      .style
                      .backgroundColor = '#' + sender.get_selectedColor();
    
  • Using jQuery Class Selector

    Get the HTMLEditorExtender by its class name ajax__html_editor_extender_texteditor. Then set its Background Color by .css() Method.

    var htmlEditorExtender = $('.ajax__html_editor_extender_texteditor');
    htmlEditorExtender.css('background-color', '#' + sender.get_selectedColor());
    

NOTE

Here we have appended ‘#’ before the Selected Color. That is because Selected Color is only a Hex Number without the prefix ‘#’, as we can see on the screenshot above, where we have alerted the Selected Color.

Thanks !!!

For taking time and reading the Blog. If you find it useful, then share within your circle, by pressing the Social Icons.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s