1

I want to be able to set a color for a color name, so if a user inputs the string 'green' the color would be a special green like #34ff8b.

I have already attempted using a variable and setting the color to the string 'green', but that is not valid js.

Desired Outcome: User inputs a color name "green" and javascript uses a special color set in the js code.

const myButton = document.getElementById('myButton');
const myHeading = document.getElementById('myHeading');
const myTextInput = document.getElementById('myTextInput');


myButton.addEventListener('click', () => {
  myHeading.style.color = myTextInput.value;
} );
@import 'https://fonts.googleapis.com/css?family=Lato:400,700';

body {
  color: #484848;
  font-family: 'Lato', sans-serif;
  padding: .45em 2.65em 3em;
  line-height: 1.5;
}
h1 {
  margin-bottom: 0;
}
h1 + p {
  font-size: 1.08em;
  color: #637a91;
  margin-top: .5em;
  margin-bottom: 2.65em;
  padding-bottom: 1.325em;
  border-bottom: 1px dotted;
}
ul {
  padding-left: 0;
  list-style: none;
}
li {
  padding: .45em .5em;
  margin-bottom: .35em;
  display: flex;
  align-items: center;
}
input,
button {
  font-size: .85em;
  padding: .65em 1em;
  border-radius: .3em;
  outline: 0;
}
input {
  border: 1px solid #dcdcdc;
  margin-right: 1em;
}
div {
  margin-top: 2.8em;
  padding: 1.5em 0 .5em;
  border-top: 1px dotted #637a91;
}
p.description,
p:nth-of-type(2) {
  font-weight: bold;
}
/* Buttons */
button {
  color: white;
  background: #1ad777;
  border: solid 1px;
  border-color: rgba(0, 0, 0, .1);
  cursor: pointer;
}
button + button {
  margin-left: .5em;
}
p + button {
  background: #52bab3;
}
.list button + button {
  background: #768da3;
}
.list li button + button {
  background: #508abc;
}
li button:first-child {
  margin-left: auto;
  background: #52bab3;
}
.list li button:last-child {
  background: #768da3;
}
li button {
  font-size: .75em;
  padding: .5em .65em;
}
<!DOCTYPE html>
<html>
  <head>
    <title>JavaScript and the DOM</title>
    <link rel="stylesheet" href="css/style.css">
  </head>
  <body>
    <h1 id="myHeading">JavaScript and the DOM</h1>
    <p>Making a web page interactive</p>
    <input type="text" id="myTextInput">
    <button id="myButton">change headline color</button>
    <script src="app.js"></script>
  </body>
</html>

6
  • 2
    but that is not valid js Huh? Commented Sep 11, 2017 at 15:48
  • var 'green' = #34ff8b; this is not valid. Commented Sep 11, 2017 at 15:50
  • The string green would actually be a valid value for element.style.color ? Commented Sep 11, 2017 at 15:51
  • 1
    @hannacreed: What do you think that line would even mean? Commented Sep 11, 2017 at 15:52
  • It sounds like you want to make a variable and an if statement to assign it. Commented Sep 11, 2017 at 15:52

3 Answers 3

5

You'll need to store your supported colors in an Object or Array.

const colors = {
    green: '#34ff8b',
    red: '#someColorValue'
}

Then when you set the style of myHeading you could say:

myHeading.style.color = colors[myTextInput.value];

That would access the colors object and pull out the value that you have pre-set.

const myButton = document.getElementById('myButton');
const myHeading = document.getElementById('myHeading');
const myTextInput = document.getElementById('myTextInput');

const colors = {
  green: 'green',
  blue: 'blue',
  red: 'red'
}

myButton.addEventListener('click', () => {
  myHeading.style.color = colors[myTextInput.value];
} );
@import 'https://fonts.googleapis.com/css?family=Lato:400,700';

body {
  color: #484848;
  font-family: 'Lato', sans-serif;
  padding: .45em 2.65em 3em;
  line-height: 1.5;
}
h1 {
  margin-bottom: 0;
}
h1 + p {
  font-size: 1.08em;
  color: #637a91;
  margin-top: .5em;
  margin-bottom: 2.65em;
  padding-bottom: 1.325em;
  border-bottom: 1px dotted;
}
ul {
  padding-left: 0;
  list-style: none;
}
li {
  padding: .45em .5em;
  margin-bottom: .35em;
  display: flex;
  align-items: center;
}
input,
button {
  font-size: .85em;
  padding: .65em 1em;
  border-radius: .3em;
  outline: 0;
}
input {
  border: 1px solid #dcdcdc;
  margin-right: 1em;
}
div {
  margin-top: 2.8em;
  padding: 1.5em 0 .5em;
  border-top: 1px dotted #637a91;
}
p.description,
p:nth-of-type(2) {
  font-weight: bold;
}
/* Buttons */
button {
  color: white;
  background: #1ad777;
  border: solid 1px;
  border-color: rgba(0, 0, 0, .1);
  cursor: pointer;
}
button + button {
  margin-left: .5em;
}
p + button {
  background: #52bab3;
}
.list button + button {
  background: #768da3;
}
.list li button + button {
  background: #508abc;
}
li button:first-child {
  margin-left: auto;
  background: #52bab3;
}
.list li button:last-child {
  background: #768da3;
}
li button {
  font-size: .75em;
  padding: .5em .65em;
}
<!DOCTYPE html>
<html>
  <head>
    <title>JavaScript and the DOM</title>
    <link rel="stylesheet" href="css/style.css">
  </head>
  <body>
    <h1 id="myHeading">JavaScript and the DOM</h1>
    <p>Making a web page interactive</p>
    <input type="text" id="myTextInput">
    <button id="myButton">change headline color</button>
    <script src="app.js"></script>
  </body>
</html>

Sign up to request clarification or add additional context in comments.

2 Comments

can you post a snippet? because this doesn't seem to be working for me
@hannacreed I reposted your snippet with this working, just change the strings in the object to whatever specific color values you need
1
myButton.addEventListener('click', () => {
  if(myTextInput.value == 'green') {
    myHeading.style.color = "#34ff8b";
  } else {
    myHeading.style.color = myTextInput.value;
  }
} );

Simple solution, the other answers also are right.

https://jsfiddle.net/Cuchu/ownfby70/

2 Comments

The only downside to this code is that if you have many colors you have to set, there would be a lot of if-then statements in your js.
If you have multiple colors use the array. Example from @DillGromble.
1

Your code is good, but you need to validate the input somehow. You can do something like this.

const myButton = document.getElementById('myButton');
const myHeading = document.getElementById('myHeading');
const myTextInput = document.getElementById('myTextInput');


myButton.addEventListener('click', () => {
  var input = myTextInput.value,
      validator = /green|red/; /* you can refine this regex to better suit your needs. */
  
  if (validator.test(input)) {
      myHeading.style.color = input;
  }
} );
<button id="myButton">Color</button>
<h1 id="myHeading">Heading</h1>
<input id="myTextInput" type="text" name="myTextInput" value="">

2 Comments

This isn't really what OP is trying to do and also validator.test('Mildred') is true here.
Thanks for the feedback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.