|
Gypsy Boy
Redeemer



Registered: 03/17/17
Posts: 4,501
Loc: Deep in the discoteka
Last seen: 2 months, 25 days
|
HTML, PHP or JavaScript ??
#26664182 - 05/11/20 10:15 AM (3 years, 8 months ago) |
|
|
I got this webpage that displays a whole bunch of "text area" boxes...
There yellow in back ground color.
HTML code is:
Quote:
<textarea name="party" cols="70" rows="12" maxlength="750" wrap="soft" class="party"> yo yo yo </textarea>
and CSS is:
Quote:
.party { color: #4865C3; font-family: Consolas, "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", Monaco, "Courier New", monospace; font-size: medium; background-color: #FAFFCD; resize: none;
What i'm tryiong to do here is make these text boxes (areas) appear with a random back ground color, a pick of say 10 colors
Any ideas how to do this? Can you do this with HTML or you need javascript/PHP?
--------------------
Edited by Gypsy Boy (05/11/20 10:59 AM)
|
Gypsy Boy
Redeemer



Registered: 03/17/17
Posts: 4,501
Loc: Deep in the discoteka
Last seen: 2 months, 25 days
|
Re: HTML, PHP or JavaScript ?? [Re: Gypsy Boy]
#26664274 - 05/11/20 11:11 AM (3 years, 8 months ago) |
|
|
There’s a nice algorithm on stack overflow:
$background_colors = array('#282E33', '#25373A', '#164852', '#495E67', '#FF3838');
$count = count($background_colors) - 1;
$i = rand(0, $count);
$rand_background = $background_colors[$i];
But how do I incorporate it into each text area/box or css ??
Should I ditch css and try to apply unique custom settings to style option of every text area?
|
singlet_oxygen
Stranger
Registered: 05/10/20
Posts: 32
Last seen: 3 years, 6 months
|
Re: HTML, PHP or JavaScript ?? [Re: Gypsy Boy]
#26664608 - 05/11/20 02:08 PM (3 years, 8 months ago) |
|
|
You really need to try and solve programming problems to learn.
I'd prefer to solve this in javascript, here's a fiddle.
HTML, we use a class called foobar for elements which should have a random background. Code:
<textarea class="foobar"> Foo </textarea> <textarea class="foobar"> bar </textarea> <textarea class="foobar"> Foobar </textarea>
Javascript we define two functions, one makes random colours, one loops through every element with class foobar. Code:
function randomColour() { let r = Math.floor(Math.random() * 256); let g = Math.floor(Math.random() * 256); let b = Math.floor(Math.random() * 256); return `RGB(${r},${g},${b})`; }
function randombg() { let tas = document.getElementsByClassName("foobar"); let elems = tas.length; let i; for(i = 0; i < elems; i++) { tas[i].style.backgroundColor=randomColour(); } } randombg();
Make sure to call randombg() somewhere, maybe in body's onLoad.
I'd do it in javascript because it's purely aesthetic and it'll be easy to change which elements have class "foobar".
Unfortunately getElementsByClassName returns a HTMLcollection not an array, or code would be neater with a map.
-------------------- 1O2
|
Gypsy Boy
Redeemer



Registered: 03/17/17
Posts: 4,501
Loc: Deep in the discoteka
Last seen: 2 months, 25 days
|
|
I removed < from php cos the forum wont allow me to post it
Thanx for helping again dude.
I just tried HTML / PHP version just for the heck of it ............. and it works!!!
?php $background_colors = array('#E19766', '#F1AECB', '#ABB4E0', '#ABE0BE', '#D4AC7F');
$count = count($background_colors) - 1;
$i = rand(0, $count);
$rand_background = $background_colors[$i]; ?>
All good.
Onto:
p style="background-color: ?php echo $rand_background; ?> ">Hmmmmmmmmmmmmmmmmm</p>
All good.
Hoefully it applies to the arera boxes well
--------------------
|
Gypsy Boy
Redeemer



Registered: 03/17/17
Posts: 4,501
Loc: Deep in the discoteka
Last seen: 2 months, 25 days
|
Re: HTML, PHP or JavaScript ?? [Re: Gypsy Boy]
#26665779 - 05/12/20 04:44 AM (3 years, 8 months ago) |
|
|
IT WORKS!!!!!!!!!!!!!!!!
No need for JAVA!!
    
--------------------
Edited by Gypsy Boy (05/12/20 05:38 AM)
|
Gypsy Boy
Redeemer



Registered: 03/17/17
Posts: 4,501
Loc: Deep in the discoteka
Last seen: 2 months, 25 days
|
Re: HTML, PHP or JavaScript ?? [Re: Gypsy Boy]
#26665805 - 05/12/20 05:32 AM (3 years, 8 months ago) |
|
|
OOhh Bay Baby Baby
Wont you feed me 
My shit works!!
--------------------
|
|