0

When the change event happens I want to create a new span element with the value on input field.

How can I style every new span element with color red for example?

If I create for example 3 spans I have to specify which one I want to style like design[0].style.color = "red"; so this one will add the color red on the 1st span, so my goal its to make all spans i create with the color = "red"

function getMessage() {
  var inel = document.getElementById("msg"),
    mainel = document.getElementById("main");


  let create = document.createElement("SPAN");
  create.innerHTML += inel.value + "<br>";
  mainel.appendChild(create)
  let att = document.createAttribute("class")
  att.value = "list"
  create.setAttributeNode(att)

  var design = document.getElementsByClassName("list");

  design.style.color = "red";

  msg.value = "";
}
<div id="main">
  <input type="text" id="msg" placeholder="type somthing here" onchange="getMessage()"><br>

</div>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Navid
  • 5
  • 2

1 Answers1

-1

Before you append the new span, add a class name to it. That way you can give all your spans the same class and stole them accordingly.

tubstrr
  • 947
  • 2
  • 9