-1

Consider the following code:

/* css */
:root {
  --text-color: #666666;
}

input {
  color: var(--text-color);
}

How do I know, using Javascript, which is the name of the CSS custom properties (variables) used?

// javascript
console.log(document.querySelector('input').style.color);
// prints "", instead of "var(--text-color) or #666666".

Context: I'm writing tests to check if the element has the proper color (or CSS variable) that it should have.

richardaum
  • 6,651
  • 12
  • 48
  • 64

2 Answers2

2

In order to get styles out of the stylesheet, you need to use getComputedStyle

var input = document.querySelector('input');
 // console.log(input.style.color); this is your original code which I have commented out as it seems to have confused you
 // prints "", instead of "var(--text-color) or #666666".

console.log(rgbToHex(window.getComputedStyle(input).getPropertyValue('color')));  
// this is the new code and prints #666666 as requested

function rgbToHex(color) {
    color = ""+ color;
    if (!color || color.indexOf("rgb") < 0) {
        return;
    }

    if (color.charAt(0) == "#") {
        return color;
    }

    var nums = /(.*?)rgb\((\d+),\s*(\d+),\s*(\d+)\)/i.exec(color),
        r = parseInt(nums[2], 10).toString(16),
        g = parseInt(nums[3], 10).toString(16),
        b = parseInt(nums[4], 10).toString(16);

    return "#"+ (
        (r.length == 1 ? "0"+ r : r) +
        (g.length == 1 ? "0"+ g : g) +
        (b.length == 1 ? "0"+ b : b)
    );
}
:root {
  --text-color: #666666;
}

input {
  color: var(--text-color);
}
<input type="submit" value="test">

The rgb to hex converter comes from here: RGB to Hex and Hex to RGB

Pete
  • 57,112
  • 28
  • 117
  • 166
-1

Here I just given sample code.

If we use the below CSS in input textbox,

.color {
    color: red;
}

<!--- Html code --->

<input type="text" name="name" id="name" class="color">

<!--- JS code --->
var cssName = document.getElementById('name').getAttribute('class');
console.log(cssName);

Hope, this will you.

jawahar N
  • 462
  • 2
  • 13
  • You aren't using CSS variables. – richardaum Oct 03 '18 at 13:09
  • color is my CSS variable – jawahar N Oct 03 '18 at 13:10
  • Nope, color is your class name. Also, red is the value of your color *attribute*. Please take a look on https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_variables. – richardaum Oct 03 '18 at 13:11
  • Actually, the question is, need to get the textbox used css variable. Right? Here, my css variable is color and using JS i have get the css variables. Is it right? – jawahar N Oct 03 '18 at 13:12
  • @jawaharN from your point of view... it might be... but `color` is not an actual _CSS variable_ here, that's a fact. – lealceldeiro Oct 03 '18 at 13:18
  • [color CSS property](https://developer.mozilla.org/en-US/docs/Web/CSS/color) vs [CSS variable](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_variables)... that's what some devs were trying to say – lealceldeiro Oct 03 '18 at 13:20
  • @lealceldeiro oh i see. Thanks for you info. Sorry thats was mistake. – jawahar N Oct 03 '18 at 14:25