I wanted to toggle between different colors for each textbox attribute as their own individual “presets”. And that, if there were multiple textboxes, the same preset would apply. It supposed to be based off this code: HTML & JS
Here’s my code below, I apologize if it’s messy for you guys :
The title
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h3>Toggle the Colors of a textbox on a Button Click</h3>
I tried to use a “color” class for it.
<input type="text" id="txt" placeholder="Type Here" class="colors"> <br>
<input type="text" id="txt2" placeholder="Type Here" class="colors"> <br>
<button id="colorButton">Click</button>
I made a “addEventListen” for the button, but maybe I should’ve just done an onClick. The textPreset variable contains the list of presets.
<script>
var textPreset = ["Blank Paper", "Calc LCD", "Blue LCD", "Red LED", "Neon Yellow"]
var i = 0
const button = document.getElementById("colorButton");
button.addEventListener("click", cyclePresets);
The cyclePresets function is suppose to use the textPreset var to correlate with the textbox attributes. preset is obviously is the value of the textPreset array.
function cyclePresets(textColor, placeHolder, textBox){
textPreset[0] = ("black", "grey", "white");
textPreset[1] = ("black", "black", "green");
textPreset[2] = ("black", "black", "blue");
textPreset[3] = ("red", "red", "black");
textPreset[4] = ("yellow", "yellow", "black");
var preset = textPreset[i];
i obviously loops through the array, tell if I should just use a for-loop.
if (i == 5){
i = 0;
}
else{
i = i + 1;
}
That textBox parameter might’ve been bad as well, as I’m with placeholder stuff, just took what I saw from tutorials and slapped on there. Then I tried to set the textboxes to the preset var, which was probably a idea lmao.
textColor = document.getElementsByClassName ("colors").style.color;
placeHolder = document.getElementsByClassName ("colors").style.backgroundColor;
textBox = document.getElementsByClassName ("colors")[0].placeholder.style.color;
document.getElementsByClassName("colors") = preset;
}
</script>
</body>
</html>
I’m going to assume I made a lot of errors, but please don’t get mad at me. I’ve just started learning these things. Maybe I’m thinking too much or I’m just too damn ambitious for my own good. Also do I need to use CSS for this? Full code here