Lajos Arpad Lajos Arpad - 1 year ago 134 Question

How to set initial color for farbtastic?

I have started to use farbtastic and the thing is brilliant. However, upon initialization it is not aware of the setColor method.

function initColorPickers(callback){
$(".picker-color-value").each(function () {
var hiddenColor = $(this).parent().find(".hidden-color-value");
var selectedColor = $(this).parent().find(".selected-color");
var currentPicker = $(this).farbtastic(function (color) {
selectedColor.css("background-color", color);
if (typeof callback === "function") {
if (typeof getColorPickerColor === "function") {
var c = getColorPickerColor(this);
selectedColor.css("background-color", c);

It says setColor is not a function. How should I set the color of my farbtastic object? For your reference, this is the ASP control I have created:

Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports DataObjects

Public Class ColorPicker
Inherits Web.UI.Control

Protected input As HtmlInputHidden

Public Sub NeedClient()
Me.Page.ClientScript.RegisterClientScriptResource(GetType(TTControls.ColorPicker), "TTControls.farbtastic.js")
Me.Page.ClientScript.RegisterClientScriptResource(GetType(TTControls.ColorPicker), "TTControls.colorPicker.js")
Dim attributes As List(Of TTPair(Of String, String)) = New List(Of TTPair(Of String, String))
attributes.Add(New TTPair(Of String, String) With {.First = "rel", .Second = "stylesheet"})
Utils.SetCSS(Me.Page, "colorPickerCSS", Me.Page.ClientScript.GetWebResourceUrl(GetType(TTControls.ColorPicker), "TTControls.farbtastic.css"), attributes)
End Sub

Public Function GetColor() As String
Return input.Value
End Function

Public Sub SetColor(ByVal color As String)
input.Value = color
End Sub

Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
input = New HtmlInputHidden()
input.Attributes.Add("class", "hidden-color-value")
Dim picker = New HtmlGenericControl("div")
picker.Attributes.Add("class", "picker-color-value")
picker.Style.Add("width", "220px")
picker.Style.Add("height", "220px")
Dim selectedColor = New HtmlGenericControl("div")
selectedColor.Style.Add("width", "100px")
selectedColor.Style.Add("height", "100px")
selectedColor.Style.Add("margin-top", "50px")
selectedColor.Style.Add("background-color", input.Value)
selectedColor.Attributes.Add("class", "selected-color")
End Sub

Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
End Sub

End Class

Let's suppose that getColorPickerColor is defined as follows:

function getColorPickerColor(picker) {
return lastColor;

This is the output html:

<div class="ui-field" style="display: flex;">
<label for="ctl00_PageBody_colorPicker" id="ctl00_PageBody_lblColor" class="ui-label">Color</label>

<input name="ctl00$PageBody$ctl00" type="hidden" class="hidden-color-value" value="#822b2b"><div class="picker-color-value" style="width:220px;height:220px;"><div class="farbtastic"><div class="color" style="background-color: rgb(255, 0, 0);"></div><div class="wheel"></div><div class="overlay"></div><div class="h-marker marker" style="left: 97px; top: 13px;"></div><div class="sl-marker marker" style="left: 97px; top: 113px;"></div></div></div><div style="width: 100px; height: 100px; margin-top: 50px; background-color: rgb(130, 43, 43);" class="selected-color"></div>

Which looks like this:

enter image description here

Answer Source

You can see from the docs that you need to call setColor method on farbtastic object:

farbtastic jquery plugin return jquery object, you need to call $.farbtastic function:

Invoking $.farbtastic(placeholder) is the same as using $(placeholder).farbtastic() except that the Farbtastic object is returned instead of the jQuery object. This allows you to use the Farbtastic methods and properties

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download