I have the buttom radio options: opc1, opc2, opc3
When I activate opc1 the div that contains the set of radio buttons gets its background totally green, and this is saved in the DB so that whenever I return it it will be green and with its appropriate radio option selected (opc1 ) and do not miss the change when the page is updated. But if in this same div I decide to click on opc2, the div changes the color and turns orange, indicating something else, saving it in BD tbm in the same way, and overwriting in BD the row that was stored in green color, and Finally, if the user clicks on opc3, the div will turn red, and the change will be saved in the DB so that it overwrites the previous one.
I'm newbie and learning in JavaScript, so I still have some difficulty in this although it seems simple, I've already looked for it a lot and I do not find anything very enlightening, I hope someone here can help me, follow below the two scripts that I did it (they are two because I tried two ways, but both failed, but I'll leave both to know which way is more correct)
First way:
<body>
<div>
<form method="post">
<label><input type="radio" name="colors" name="green" onClick="changeColor('g')"> Verde <br></label>
<label><input type="radio" name="colors" name="orange" onClick="changeColor('o')"> Laranja <br></label>
<label><input type="radio" name="colors" name="red" onClick="changeColor('r')"> Vermelho <br></label>
</form>
</div>
Script way 1:
function changeColor(color){
var color = document.div.style.backgroundColor;
switch(value){
case 'g':
color = "#6F8456";
break;
case 'o':
color = "#FA7921";
break;
case 'r':
color = "#670000";
break;
}
document.div.backgroundColor = color;
}
Second way:
<div>
<form method="post">
<label><input type="radio" name="colors" name="green" onClick="changeColour('g')"> Verde <br></label>
<label><input type="radio" name="colors" name="orange" onClick="changeColour('o')"> Laranja <br></label>
<label><input type="radio" name="colors" name="red" onClick="changeColour('r')"> Vermelho <br></label>
</form>
</div>
Second way script:
function changeColour(){
if("g")
document.div.style.backgroundColor="#6F8456";
else
document.div.style.backgroundColor="#ffffff";
if("o")
document.div.style.backgroundColor="#FA7921";
else
document.div.style.backgroundColor="#ffffff";
if("r")
document.div.style.backgroundColor="#670000";
else
document.div.style.backgroundColor="#ffffff";
}
If possible send the code commenting for me to try to understand what changes have been made to what, and also to do the code in JavaScript and NOT in Jquery.