When trying to change color of label controls, the color changes for one label (the last label) when selecting color from color <input>; other labels do not work. All the color <input> work for ONLY one label. It is like all the <input> are attached to one
label control, but I made sure that the label id and <input> id belong to each control. Please can anyone help out with solution to this?
I want to set <input> color to each label so that when I select color from the <input> for each label, the label color changes. Now the problem I have is that when I select color, it only applies to one label control...The other label color does not change,
even though I assigned an <input> color to it
You have a lot of things wrong. I'll ask you to look at them, and my sample correction for two labels at the end, and try again.
First, this function is wrong:
function updateFirst(event) {
var label = document.querySelector("Label2");
if (Label2) {
Label2.style.color = event.target.value;
}
}
You are trying to select an id #Label2, but missing the #.
You have defined a variable "label", and not used it. Instead you are using the string "Label2" in your if statement.
You have the same problem in your next "updateFirst" function.
function updateFirst(event) {
var p = document.querySelector("Label1");
if (Label1) {
Label1.style.color = event.target.value;
}
}
Again you have a string where you need an id (#Label1), you've defined a variable you aren't using (p), and you are using a string rather than the variable in your if statement.
This brings up another major problem. You are writing functions for multiple labels and color pickers, but using the same function names.
In JavaScript if you define multiple functions with the same name, then the last one defined will be the one that actually runs.
Change the names, and the references to those names. You don't need to put the Javascript in separate <script> tags, but if you want to for your own clarity, that's up to you. Just don't think it keeps the names of functions and variables separate - it
doesn't.
Here is a redo of your script for the first two labels. Follow it to add the additional code for the other two labels.
<script>
var namecolor;
var defaultColor = "#0000ff";
window.addEventListener("load", startupname, false);
function startupname() {
namecolor = document.querySelector("#namecolor");
namecolor.value = defaultColor;
namecolor.addEventListener("input", updateFirstname, false);
namecolor.select();
}
function updateFirstname(event) {
var labelname = document.querySelector("#Label1");
if (labelname) {
labelname.style.color = event.target.value;
}
}
</script>
<script>
var role;
var defaultColor = "#0000ff";
window.addEventListener("load", startuprole, false);
function startuprole() {
role = document.querySelector("#role");
role.value = defaultColor;
role.addEventListener("input", updateFirstrole, false);
role.select();
}
function updateFirstrole(event) {
var labelrole = document.querySelector("#Label2");
if (labelrole) {
labelrole.style.color = event.target.value;
}
}
</script>
You need to step back and think about how this works, and provide the correct code for all the labels.
I want to set <input> color to each label so that when I select color from the <input> for each label, the label color changes. Now the problem I have is that when I select color, it only applies to one label control...The other label color does not change,
even though I assigned an <input> color to it
I think you are trying to do something like the following. The idea is crafting HTML to make the design a bit easier to handler. The data attribute is used to map the color input to the label. A CSS class, color-picker, is used to select multiple color
inputs.
Keep in mind Web Form IDs are not static. The ID selector(s) will break if the labels are placed inside another web forms control. I recommend moving to MVC if you are interested in learning JavaScript. Web forms are not JavaScript friendly.
Member
66 Points
175 Posts
Changing label color for each label control
Oct 03, 2020 06:00 PM|georgeakpan233|LINK
Hello,
When trying to change color of label controls, the color changes for one label (the last label) when selecting color from color <input>; other labels do not work. All the color <input> work for ONLY one label. It is like all the <input> are attached to one label control, but I made sure that the label id and <input> id belong to each control. Please can anyone help out with solution to this?
Here is my HTML and Javascript
All-Star
53091 Points
23659 Posts
Re: Changing label color for each label control
Oct 03, 2020 06:18 PM|mgebhard|LINK
I'm having a hard time understanding the intent by reading the code. Can you explain the programming problem you are trying to solve?
Member
66 Points
175 Posts
Re: Changing label color for each label control
Oct 03, 2020 06:37 PM|georgeakpan233|LINK
Mgebhard,
I want to set <input> color to each label so that when I select color from the <input> for each label, the label color changes. Now the problem I have is that when I select color, it only applies to one label control...The other label color does not change, even though I assigned an <input> color to it
Contributor
5961 Points
2468 Posts
Re: Changing label color for each label control
Oct 03, 2020 07:14 PM|KathyW|LINK
You have a lot of things wrong. I'll ask you to look at them, and my sample correction for two labels at the end, and try again.
First, this function is wrong:
You are trying to select an id #Label2, but missing the #.
You have defined a variable "label", and not used it. Instead you are using the string "Label2" in your if statement.
You have the same problem in your next "updateFirst" function.
Again you have a string where you need an id (#Label1), you've defined a variable you aren't using (p), and you are using a string rather than the variable in your if statement.
This brings up another major problem. You are writing functions for multiple labels and color pickers, but using the same function names.
In JavaScript if you define multiple functions with the same name, then the last one defined will be the one that actually runs.
Change the names, and the references to those names. You don't need to put the Javascript in separate <script> tags, but if you want to for your own clarity, that's up to you. Just don't think it keeps the names of functions and variables separate - it doesn't.
Here is a redo of your script for the first two labels. Follow it to add the additional code for the other two labels.
You need to step back and think about how this works, and provide the correct code for all the labels.
All-Star
53091 Points
23659 Posts
Re: Changing label color for each label control
Oct 03, 2020 07:48 PM|mgebhard|LINK
I think you are trying to do something like the following. The idea is crafting HTML to make the design a bit easier to handler. The data attribute is used to map the color input to the label. A CSS class, color-picker, is used to select multiple color inputs.
Keep in mind Web Form IDs are not static. The ID selector(s) will break if the labels are placed inside another web forms control. I recommend moving to MVC if you are interested in learning JavaScript. Web forms are not JavaScript friendly.
Member
66 Points
175 Posts
Re: Changing label color for each label control
Oct 03, 2020 08:28 PM|georgeakpan233|LINK
Thank you KathyW. I am new to java, so I really don't know much. I am learning
Member
66 Points
175 Posts
Re: Changing label color for each label control
Oct 03, 2020 08:30 PM|georgeakpan233|LINK
Hi mgebhard,
Thanks. I understand. I will check out MVC
Contributor
5961 Points
2468 Posts
Re: Changing label color for each label control
Oct 04, 2020 02:09 PM|KathyW|LINK
Actually, there is quite a difference between Java and JavaScript. We are talking about JavaScript here, not Java. Different language entirely.