How do I access JavaScript variable under Color portion of HTML code?

use javascript variable in html attribute
store html in javascript variable
assign html element to javascript variable
how to display javascript variable value in html page
store html in variable jquery
display javascript variable in html div
javascript variables

I would like to access the variable "theSkyColour" inside the HTML code below as shown. May I know how do I access it as shown?

The following is the code I have written:

function getSkyColour() {
  var theSkyColour = localStorage.getItem('skySet[enter image description here][1]ting');
<a-sky id="skyColor" src="#sky" 
material="shader:gradient; topColor: <<I want to access theSkyColor here>>; bottomColor: <<I want to access theSkyColor here>> ; offset:0;"></a-sky>

Unless you are using a framework like Angular or django, it is not possible to access variables in your HTML code, instead you can construct the attribute in your Javascript and add it to the element.


var myAttr = "shader:gradient; topColor:"+skyColor+"; bottomColor:"+skyColor+"; offset:0;"

Update: without using jQuery:

function getSkyColour() {
  var theSkyColour = localStorage.getItem('skySetting');

var myAttr = "shader:gradient; topColor:"+theSkyColour+"; bottomColor:"+theSkyColour+"; offset:0;"


HTML var tag, How do you display the value of a variable in HTML? If you want to change the text inside another paragraph, then first give the paragraph an id, then set a variable to it using getElementById([id]) to access it ([id] means the id you gave the paragraph). Next, use the innerHTML built-in variable with whatever your variable was called and a '.' (dot) to show that it is based on the paragraph. You can set it to whatever you want, but be aware that to set a paragraph to a tag (<>), then you have to still put it in speech marks.

check out the snippets for getting and setting attributesin an a-frame component. Refer to the following documentation:

It explains very well how to get and how to set values from component properties and sub-properties:

Get Property

Set Property

Html tag inside javascript, in programming or in a mathematical expression. The content inside is typically displayed in italic. Answer: Use the concatenation operator (+) The simple and safest way to use the concatenation operator ( +) to assign or store a bock of HTML code in a JavaScript variable. You should use the single-quotes while stingify the HTML code block, it would make easier to preserve the double-quotes in the actual HTML code.

You can’t, but you can change the function to update the color of the sky.

document.getElementById("skyColor").material = `defaultStylesInHere topColor:${localStorage.getItem("skyColor")};`;


let setSkyColor = (val) => {
    localStorage.setItem('skyColor', `defaultStylesInHere topColor:${val};bottomColor:${val}`);
let loadSkyColor = () => {
    document.getElementById('skyColor').attributes.material = localStorage.getItem('skyColor');

I can make full functions, but it should not be necessary You could also use JSON or Objects to set and grab sky properties

let app = {
    skyProp: {
        color: 'rgba(200, 200, 255, 1)',
        color: {
            default: {
                color: 'rgba(200, 200, 255, 1)',
                topColor: 'green',
            night: {
                color: 'rgba(255, 255, 255, 1)',
                bottomColor: 'rgba(155, 155, 155, 1)',

<var>: The Variable element, How do you assign a block of HTML code to a JavaScript variable? However the most preferred ways to include JavaScript in an HTML file are as follows −. Script in <head></head> section. Script in <body></body> section. Script in <body></body> and <head></head> sections. Script in an external file and then include in <head></head> section.

How to Assign Block of HTML Code to a JavaScript Variable, in a mathematical expression or a programming context. It's typically presented using an italicized version of the current typeface, although that behavior is browser-dependent. The var () Function. Variables in CSS should be declared within a CSS selector that defines its scope. For a global scope you can use either the :root or the body selector. The variable name must begin with two dashes (--) and is case sensitive! The syntax of the var () function is as follows: var ( custom-name, value) Value. Description.

Communicating Between JavaScript and CSS Using CSS Variables , The simple and safest way to use the concatenation operator ( + ) to assign or store a bock of HTML code in a JavaScript variable. You should use the  JavaScript variables can hold numbers like 100 and text values like "John Doe". In programming, text values are called text strings. JavaScript can handle many types of data, but for now, just think of numbers and strings. Strings are written inside double or single quotes. Numbers are written without quotes.

JavaScript String fontcolor() Method, As of 2016, CSS Variables are fully usable in Chrome and Firefox, and IE has the advantage of this is obvious, you can easily change colors, sizes, etc. in a html { --header-height: 40px } .page-content { top: calc(var(--header-height) + an interesting use of CSS Variables to communicate between JavaScript and CSS. using Javascript variables in HTML code n00bdanimal I'm attempting to take a javascript variable and input into some HTML schtuffbut for some reason it doesn't seem to be working.

  • It's not possible to access javascript variable in html.
  • check this:
  • "It did not work for me" how? It is not very descriptive. Please update the snippet I made for you with your attempts - Also please tell us what framework has <a-sky tags
  • Tri this: document.getElementById('skyColor').setAttribute('material', document.getElementById('skyColor').getAttribute('style')+'; color: '+getSkyColour());
  • For the second line "$("#skyColor").attr("material",myAttr)" , may I know where should I place this line of code? Is this a PHP code? How do I use it?
  • This is a jQuery code, you need to have jQuery library in your application to use it. If you do not have jQuey library, I will suggest an alternative to it in pure JavaScript
  • I would like to use pure Javascript as well. However, like I mentioned in the original post, there is a problem of being unable to call it inside the HTML code with Javascript. So I'm confused. What should I do here if I would like to use pure Javascript?
  • Basically, any JavaScript you use goes inside "<script> your JavaScript </script>"
  • Hi, I've edited the code according to what you've specified. Unfortunately, the display still does not update according to the desired preferences. I have edited the original post to include the results shown on the browser, along with the codes I have used in the HTML code. Could you advise? Thank you so much for your help
  • Does this apply in my scenario, considering that I am accessing a local session? I am using some aframe components, however, I am not sure if it works since I am changing a local session on another HTML page. Could you advise if there is an alternative for using local sessions storage?