-3

I want to add color and border to a javascript variable using css. Below is my code;

var msg = "OK"; //i want this to colored and bordered with green.
msg = "Conflict"; // i want this to be colored and bordered with red.

I tried another answer from other questions but it doesn't seem to work with me.

6
  • 3
    Hmmm, that's not how Javascript works :) Commented Mar 11, 2019 at 8:01
  • you can't apply css to js but you can use js to style html elements. Commented Mar 11, 2019 at 8:02
  • @julekgwa, how? Commented Mar 11, 2019 at 8:02
  • That depends on how this variable gets used, how exactly it is made to display its value and where. As long as it is only an abstract internal object, it does not even make sense to talk about it having any “color”. Commented Mar 11, 2019 at 8:04
  • 1
    If you write var msg = "OK" it means that the variable msg, in your computer's memory somewhere, holds the value "OK". Nothing else. It has no concept of color or size or psychology or anything. It's just a value. But you can write this value to your HTML document, like in a <div> or something, and then use CSS to style that div and give it a color and a size. This is day 1, hour 1 of web development, I suggest you take some very basic tutorial :) Commented Mar 11, 2019 at 8:06

3 Answers 3

0

If you're just trying to add styles to a JavaScript variable you can't do that, and I don't understand what you would hope to achieve by doing that.

I am therefore going to assume you want to add styles to an html element that you have extracted as a JavaScript variable like so

let msgElement = document.getElementById('msg')
let msg = "OK"
msgElement.innerHTML = msg

In this case, you can add styles to the element like so

msgElement.style.color = "red"
msgElement.style.border = "2px solid red"

In your example, when you change the value of msg to "Conflict", you are doing just that - changing it. You can't have two separate values held by the same variable.

As one of the comments says, this is basic web development, so I would advise some further reading, or an online course such as those provided by Codeacademy

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

3 Comments

it does nothing
What exactly are you trying to achieve?
You really only need to show the snippet like the one in my answer, add the extra code to the question.
0

As the other answers state, you can't apply a CSS rule to a variable. You can, however, do something like this:

<html>
<head>
<style>
.redgreen {border-style: solid; border-color: green; color: red;}
</style>
<script>
function foo() {
    let msg = "<div class='redgreen'>Hello, world!</div>";
    document.getElementById("themsg").innerHTML = msg;
    }
</script>
</head>
<body onload='foo();'>
<p id='themsg'>Your message here</p>
</body>
</html>

That is, define "msg" as an HTML element instead of a text string.

3 Comments

This doesn't seem like good practice to me. Using .innerHTML to add a div to a p element is not a good idea as p elements shouldn't really have any children other than inline elements such as span
So change the <p> to something else! The point of my answer is that he can style his message by coding a class (or whatever -- there are many ways of invoking a CSS rule) as part of the text. He doesn't need to write code to style the object he's replacing.
My point is you shouldn't use innerHTML like that. But you're right, for the purpose of this question it doesn't matter.
0

You can't add CSS to a javascript variable.

if you are create element using javascript

html:

<div class="parent-div">

</div>

js:

var msg = "OK";
element = document.createElement('p');

// Give the new element some content and css
element.innerHTML = msg;
element.style.color = 'green';
element.style.border = "1px solid red";

// append element to parent div
document.querySelector('.parent-div').appendChild(element);

Just do without javascript

html:

<div class="parent-div">
  <p class="child-one">OK</p>
  <p class="child-two">Conflict</p>
</div>

css:

.parent-div .child-one {
  color: red;
  border: 1px solid green;
}

.parent-div .child-two {
  color: green;
  border: 1px solid red;
}

2 Comments

I was about to say "this is a comment, not an answer", but it is actually a correct answer. However you should also guide OP on how to achieve what they want
but sir, i have not so much reputation to comment

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.