0

I am a newbie to the whole html,css and javascript world. i am required to create a button which changes the color of a text in a span based of the value that is written within a textbox.

This is what i managed to write so far:

function changeColor() {
  var span = document.querySelector("span");
  if (span = 'purple') {
    span.style.color = 'purple';
  }
}
.color {
  color: white;
  background-color: black;
}

.red {
  background-color: red;
}

.blue {
  background-color: blue;
}

.yellow {
  background-color: yellow;
}

.purple {
  background-color: purple;
}
<form>
  <span>This text changes color</span>
  <div>
    <input type="text" value="Type your input here">
  </div>
  <div>
    <input type="button" value="Add Color" onclick="changeColor()">
    <input type="button" value="Clear all colors" onclick="ClearColors()">
  </div>
</form>
<script type="text/javascript" src="Homework3.js"></script>

0

2 Answers 2

2

You can use the value of the text input as the class name. Then you can use classList.add() property to add the class on the span element. You also do not need the clear button. You can use classList.remove() property to remove the class in the same function.

Try the following way:

var span = document.querySelector("span");
var prevColor = '';
function changeColor() {
  var currentColor = document.querySelector("input[type='text']").value.trim();
  if(prevColor != '')
    span.classList.remove(prevColor);
  if(currentColor != ''){
    span.classList.add(currentColor);
    prevColor = currentColor;
  }
}
.color {
    color:white;
    background-color: black;
}
.red {
    background-color: red;
}

.blue {
    background-color: blue;
}

.yellow {
    background-color: yellow;
}

.purple {
    background-color: purple;
}
<span>This text changes color</span>
<div>
    <input type="text" placeholder="Type your input here">
</div>
<div>
    <input type="button" value="Add Color" onclick="changeColor()">
</div>

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

Comments

1

Simple Solution It is not a good design pattern to hard-code your color classes in the css file since its the user that determine the needed color. You will limit the user's option by hard-coding the colors. what if the user key-in a color not in the css file. In view of the above, i created this solution:

Change the value attribute in input tag to placeholder i.e

From <input type="text" value="Type your input here">
to   <input type="text" placeholder="Type your input here">

Remove this from your html. its not necessary

<input type="button" value="Clear all colors" onclick="ClearColors()"> 

Replace the code in your javascript function with this

var span = document.querySelector("span");  
  var color = document.querySelector("input").value.trim();  
  span.style.backgroundColor = color;

Here is the full source code:

function changeColor() {
  var span = document.querySelector("span");
  var color = document.querySelector("input").value.trim();
  span.style.backgroundColor = color;
  //If you want to alter the color of text
  //span.style.color = 'any color';  
}
<head>
  <title>Inputs and forms</title>
</head>

<body>
  <form>
    <span>This text changes color</span>
    <div>
      <input type="text" placeholder="Type your input here">
    </div>
    <div>
      <input type="button" value="Add Color" onclick="changeColor()">
    </div>
  </form>
</body>

NOTE: CSS not needed

Comments

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.