EastDesire/jscolor

how can I trigger jscolor open and close by js code

jyjin opened this issue · 3 comments

jyjin commented

I didn't want to operate the jscolor with that input, maybe from another button, what should I do?

Hi jyjin,
see:
http://jscolor.com/examples/#example-showing-hiding

<script src="jscolor.js"></script>

<button onclick="document.getElementById('foo').jscolor.show()">Show Picker</button>
<button onclick="document.getElementById('foo').jscolor.hide()">Hide Picker</button>

<p><input id="foo" class="jscolor" value="cc4499">

Jan

jyjin commented

Thank you for your answer. I already has a solution which by trigger event, cause i cant use js code in the dom elements. and i found if the elements created by js code, jscolor didn't work, it must be pre set the element to the dom.and my code:

set open:

var event = document.createEvent('HTMLEvents');
event.initEvent('mousedown', true, true);
event.initType = 'msg';
txt.dispatchEvent(event);

Init a color for jscolor:

var input = document.getElementsByTagName('input')[0];
input.value = '000000';
var event1 = document.createEvent('HTMLEvents');
event1.initEvent('keyup', true, true);
event1.initType = 'msg';
input.dispatchEvent(event1);

Follow your answers, how set init color by element binding?

I think you need to do something like:

var picker = new jscolor(input);
picker.show();