user9157270
user9157270

Reputation:

Inline CSS is not working when referenced from javascript

I feel as though this should work. I'm creating a variable in javascript and referencing it in the same file. Can someone explain why this is not working and then show how I can achieve my intended result? I feel as though I'm missing something obvious.

*edit: I forgot to mention that it is important that I use a javascript variable as my color. Sorry.

<!DOCTYPE html>
<html>

<head>
</head>

<body style="background-color:color;">
</body>
<script type="text/javascript">
	var color = #555555;
</script>

</html>

Upvotes: 0

Views: 34

Answers (2)

Earl. Fojas
Earl. Fojas

Reputation: 89

You can instead use this on your javascript:

  var color = '#555555';
  document.body.style.backgroundColor = color;

Upvotes: 0

Andy Holmes
Andy Holmes

Reputation: 8047

Your example of use isn't how browsers work. If you wanted to achieve your example properly you could try

<script type="text/javascript">
    var color = '#555555'
    document.body.style.backgroundColor = color;
</script>

https://jsfiddle.net/8dgoddw8/ - shows it working

Upvotes: 1

Related Questions