Jump to content

Preview colors from textfield?


Jason28

Recommended Posts

Hello, I read this in a book and it looked like very simple code.  I would like to be enter a hex color in a field like #000000, and have it display that color in a <span> or <div> beside that field automatically.  Could you please provide me with a code example that does this?  Thanks!

Link to comment
https://forums.phpfreaks.com/topic/230866-preview-colors-from-textfield/
Share on other sites

You can add an onchange event and set the div background color style to that value

 

e.g.

<input type="text" onchange="document.getElementById('my_div_id').style.backgroundColor = this.value;" /><div id="my_div_id"> </div>

 

This code won't validate the user input and an invalid color will cause an error.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.