Search code examples
javascriptjqueryjscolor

Changing JSColor value via Javascript / Jquery


How can you set value of the input text box (which is of JSColor class) via javascript?. I would like to change both background color and input value (via Javascript command, or Jquery. good enough).

Thank you.


Solution

  • Using jquery

    var yourinputid = $('#YOURINPUTID') //save some resources by not duplicating the selector  
    yourinputid.val('yourcolor'); //set the value
    yourinputid.css('background-color', '#yourcolor'); //set the background color
    

    Without jquery

    var yourinputid = document.getElementById('YOURINPUTID'); //save resources..
    yourinputid.value = 'yourcolor'; //set the value
    yourinputid.style.backgroundColor = '#yourcolor'; //set the background color
    

    Note that you should not use the # at the start of the color value when setting it to the object.