Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger click on HTML 5 color picker with JQuery

Within JQuery we are able to trigger 'click' on any given element with:

$(selector).trigger('click');

Though i have difficulties doing so when the element is a HTML 5 color picker and the 'display' property is set to 'none' by CSS.

Normally we can do this if the input type is 'file' or ...

So is there anyway to get this done with JQuery or plain javascript?

HTML elements

input type="color" id="fontSel" style="display: none;"
div id="fontWrap"

JQuery

$("#fontWrap").click(function(){
     $("#fontSel").trigger("click");
});



ANSWER

Change the style to:

input type="color" id="fontSel" style="position:absolute; left:-9999px; top:-9999px;"
like image 205
mdoust Avatar asked Jul 18 '13 16:07

mdoust


1 Answers

This need user interaction, meaning you cannot trigger it without user clicking somewhere in document:

DEMO

$("#fontWrap").click(function () {
    $("#fontSel")[0].click();
});
like image 76
A. Wolff Avatar answered Sep 22 '22 01:09

A. Wolff