Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a value from HTML5 color picker

I am trying to change the color of an h1 element based on a selection using the color input type, but i cant seem to get it to work. I've tried outputting the color variable to an alert and it only returns undefined.

 $(function(){

        $("#changeColor").click(function(){

          var color =  $("#colorChoice").value

          $("h1").css("color" + '"' + color + "'")


        });



    });


    </script>


</head>
  <html>
  <body>

  <h1>This is the default text</h1>
  <form>
  <fieldset>
      <select id="changeFont">
        <option>Arial</option>
        <option>Georgia</option>
        <option>Helevtica</option>
    </select>
    <input type="color" id="colorChoice">
    <button id="changeColor" class="btn-primary pull-right">Change</button>
</fieldset>
</form>
like image 975
Alex Hill Avatar asked Oct 31 '12 08:10

Alex Hill


1 Answers

Try var color = $("#colorChoice").val(); See jQuery docs

You might as well bind your callback on the onChange event instead of the onClick event:

$("#colorChoice").change(function(){
  $("h1").css('background', $(this).val());
});

Working sample

like image 113
Vincent Mimoun-Prat Avatar answered Sep 20 '22 21:09

Vincent Mimoun-Prat