Detect Changes to Spectrum Colorpicker

Introduction

In this blog, we will see how to manage the default value of color picker so that we can detect if color is changed.

Background

In my current project, requirement came to check if there are changes on the page on a button click and show a modal alerting the user that “There are changes, do you want to save?”. I found a script which can be triggered to check the changes on the page. It worked perfectly with all input, select and textarea controls.

But if I have a colorpicker on the page, then it always told me that you have changes, even if I did not change the color in the picker. Let me explain more. The colorpicker used is Spectrum.

Step by Step

Suppose, I have one input, select and textarea on my page. One button to check if we have any change on these controls meaning if we edited/changed the field values.

<input />

<select>
    <option value="1">1</option>
    <option value="2" selected="selected">2</option>
    <option value="3">3</option>
</select>

<textarea>Hello World</textarea>

<button onclick="CheckForChanges()">
    Check For Changes
</button>

Now let’s check the click event code.

function CheckForChanges() {
  if (GetHasChanges()) {
      alert("Yes we have changes!!!");
  } else {
      alert("No changes!!!");
  }
}

I am not posting the GetHasChanges function here, you can see in the jsfiddle Demo.
So, see the picture below which tells what is before you click the button and what happens after you edit some field and click the button. It perfectly checking that we have a change.

Before and After Changes Done

Before and After Changes Done

With Colorpicker

Let’s add a colorpicker now.

<input id="colorpicker" style="display:none;" value="#000" />
$(document).ready(function() {
  $('#colorpicker').spectrum({
    preferredFormat: "hex"
  });
});

Demo – Demo With a Colorpicker
As you see in the picture below, everything is perfect.

Detecting Changes with Colorpicker

Detecting Changes with Colorpicker

Implementing Input Field inside the Colorpicker

This can be done by setting the showInput property to true

$(document).ready(function() {
  $('#colorpicker').spectrum({
    showInput: true,
    preferredFormat: "hex"
  });
});

Spectrum Colorpicker With ShowInput True

Spectrum Colorpicker With ShowInput True

What we get by doing this? Just a new input field inside the colorpicker so that we can give color hex.

Let’s get back to what we were doing.
OOPS!!! With no change, it is now saying that “Yes we have changes!!!”. See the picture below.

Checking changes with ShowInput true in Spectrum Colorpicker

Checking changes with ShowInput true in Spectrum Colorpicker


Demo – Demo to check changes with ShowInput true in Spectrum Colorpicker
So, I have not picked any color, still its detecting changes. We will find out the problem in next paragraph.

Research

I started research from Developer tool and debugged the method which is checking the changes. What I found, was very interesting.

Debugging GetHasChanges Function in Developer Tool

Debugging GetHasChanges Function in Developer Tool


So, as you see, it is detecting the change for an input. I, then, found it inside the HTML.

<input class="sp-input" type="text" spellcheck="false">

This is the same input which is inside the colorpicker. The problem it detected a change because it does not have a default value in place and value given to it is the color we provided (#000).

Solution

Have you guessed the solution!!! It’s easy. We just need to provide the default value as the same color value. Thus, it will only detect the change when an actual change is made. The final code looks like below. Highlighted the codes used to assign the default value.

$(document).ready(function() {
  $('#colorpicker').spectrum({
    showInput: true,
    preferredFormat: "hex"
  });

  $('.sp-container .sp-input').each(function() {
    this.defaultValue = this.value;
  });
});

Demo – Final Demo Detecting Colorpicker Change

Feedback

I would definitely appreciate, if you give feedback to my blog. If you find it interesting and useful, then please share with your friends and colleagues.

Thanks for reading, have a nice day!!!

Advertisements

Rounded Button for jQuery ColorPicker

Square to Rounded Button

Square to Rounded Button

Introduction

In this blog, we will explore the trick to replace the default square ColorPicker Button with a rounded one.

Background

One recent requirement lead to this research, where rounded button was needed instead of the default square one.

Step by Step

We will use ColorPicker plugin for our task. Let’s start coding.

Step-1: Add Plugin References

You need to refer jQuery and ColorPicker Plugin references.
We need

  1. jQuery
  2. jQuery UI (js and CSS)
  3. ColorPicker (js and CSS)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/evol.colorpicker.min.js" type="text/javascript" charset="utf-8"></script>

<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/ui-lightness/jquery-ui.css">
<link href="css/evol.colorpicker.css" rel="stylesheet" type="text/css">

You can use the online googleapi links, otherwise download and use from the official jQuery Site. For ColorPicker, Save the files from GitHub Source and use it in your project. According to the code, there is a js folder, where evol.colorpicker.min.js is present and css folder, where evol.colorpicker.css is stored. Please be careful with the paths to the files, otherwise it won’t work.

Step-2: Add Input Control for ColorPicker

Just add a TextBox with HTML input tag.

<input type="text" id="mycolor" />

Step-3: Script for ColorPicker

$(document).ready(function () {
    // Initiate the ColorPicker
    $("#mycolor").colorpicker({
        color: "#ff0000"
    });
});

Now, if you click on the TextBox or the “square button”(next to the TextBox), it will show you a widget below to that to pick a color. Picture shown below.

ColorPicker Square Button

ColorPicker Square Button

Step-4: Inspect the HTML ColorPicker Source

When you inspect the area which shows the ColorPicker along with the TextBox, you will get the below div.

<div style="width:201px;">
    <input type="text" id="mycolor" class="colorPicker evo-cp0">
    <div class="evo-pointer evo-colorind" style="background-color:#ff0000"></div>
</div>

So, it surrounded the input with a div and inside that it added another div (see highlighted), which is the button. Now we just need to change it to round. Is it easy? Let’s explore in the next step.

Step-5: Assign Border Radius to the Button Div

Let’s add CSS property border-radius to the button div having classes evo-pointer evo-colorind. I have just selected the div by one class, that is evo-pointer

$(document).ready(function () {
    // Initiate the ColorPicker
    $("#mycolor").colorpicker({
        color: "#ff0000"
    });

    // Add radius to the div containing the button
    $(".evo-pointer").css("border-radius", "15px");
});

The HTML changes to the below with border-radius: 15px; added to the style.

<div style="width:201px;">
    <input type="text" id="mycolor" class="colorPicker evo-cp0">
    <div class="evo-pointer evo-colorind" style="border-radius: 15px; background-color: rgb(255, 0, 0);">
    </div>
</div>

Wow !!! It is done. See the picture below.

ColorPicker Rounded Button

ColorPicker Rounded Button


Is it done? No, not yet. Hold on. Let’s test. If you select any color from the widget, then you will see that the button again reverts to the square one. The reason is the button div is dynamically assigned each time you select any color from the picker. The HTML reverts back to the original.

<div style="width:201px;">
    <input type="text" id="mycolor" class="colorPicker evo-cp0">
    <div class="evo-pointer evo-colorind" style="background-color:#f79646">
    </div>
</div>

Here border-radius: 15px; is now missing.
Watch how the button is changing again to square in below gif.

Rounded Button Changed to Square

Rounded Button Changed to Square

Step-5: Handle Change Color Event

So, let’s handle the Change Color Event to turn the shape of the button to round again.

$(document).ready(function () {
    // Initiate the ColorPicker
    $("#mycolor").colorpicker({
        color: "#ff0000"
    });

    // Add radius to the div containing the button
    $(".evo-pointer").css("border-radius", "15px");

    // Set the radius again in ColorPicker change color event
    $(".colorPicker").on("change.color", function (event, color) {
        $(this).next().css("border-radius", "15px");
    });
});

We are assigning css property to the next element of the ColorPicker TextBox, which is the button.

Demo

[Demo] Rounded ColorPicker Button

Conclusion

I hope you enjoyed reading this blog. If you have questions, please comment below. I will try to answer. If you like it, please share on your social channels.

Thanks for reading. Have a nice day. 🙂

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.