Using Bootstrap Colors in JS
Asked Answered
V

2

3

I am using a Google-Chart and want to set the slice colors to the color used by bootstrap, e.g., for badge-success or badge-danger. Is there any way to access these color-codes from within JavaScript?

Volturno answered 26/8, 2018 at 9:37 Comment(0)
E
5

This works if you're using Bootstrap 4:

const root = document.querySelector('html');
getComputedStyle(root).getPropertyValue('--danger');

You can access the CSS variables present in the :root pseudoelement

Etka answered 26/8, 2018 at 10:7 Comment(3)
Working nicely thanks. also tried $('.badge-warning').css("backgroundColor") which does the job too if I really need to access some specific class and CSS featureVolturno
Yes - but you'd need to have one of those badge-warning elements on every page where you have the chart. Otherwise that jquery call will return undefined.Etka
Oh, I didn't think about that. That would've been quite a surprise. Thanks for the hint!Volturno
E
3

Another approach is...

document.body.innerHTML += '<div id="myBadge" class="badge-danger"></div>';
var elem = document.getElementById("myBadge");
var dangerColor = window.getComputedStyle(elem).getPropertyValue("background-color");
elem.parentNode.removeChild(elem);

Demo: https://www.codeply.com/go/GC96hjbf5v

Electrode answered 26/8, 2018 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.