Search code examples
javascriptjscolor

Change text color with jscolor


I use http://jscolor.com/ for color manipulations and I need to be able to change text color in this button <a href="#" id="button_cont" >Call to action</a>. So far I only found this piece of code close to my situation but it's not working for me either.

<script type="text/javascript">

     function update(jscolor) {
    $('#button_cont').css("color", "#" + jscolor);}

    </script>


    <input type="text" class="jscolor" onchange="update(this.jscolor);" /> 

<a href="#" id="button_cont" >Call to action</a>


Solution

  • You can use onchange callback to update your button text color.

    <body>
      <input type="text" class="jscolor" onchange="update(this.jscolor);" />
      <a href="#" id="button_cont">Call to action</a>
      <script type="text/javascript">
        function update(jscolor) {
          $("#button_cont").css("color", "#" + jscolor);
          // document.getElementById('button_cont').style.color = '#' + jscolor;
        }
      </script>
    </body>
    

    Remember to include your script import

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

    and jQuery of course

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>