Access HTML tag rules [duplicate]

My question is if there is a way to access the content of HTML <style> element from jQuery.

<style>
.calendarNavbarLink {
    margin-left: 20px;
}

.today {
    background-color: lawngreen;
}

.weekend {
    background-color: lightsalmon;
}
</style>

For example: I would access the value of the property background-color of the rule .weekend. Is it possible do this from jQuery?

2

Step 1:

By using a couple of CSSOM (CSS Object Model) properties, you can query your stylesheet and build an easy-to-search array of selectors and style rules:

// GET ALL STYLESHEETS
let styleSheetCount = document.styleSheets.length;

// GET LAST STYLESHEET
let lastStyleSheet = document.styleSheets[(styleSheetCount - 1)];

// CONVERT STYLESHEET TO OBJECT
const convertStylesheetToObject = (stylesheet) => {
  
  // CREATE AND POPULATE STYLE ARRAY
  let styleArray = [];
  [...stylesheet.cssRules].forEach((cssRule) => styleArray.push(cssRule.cssText));

  styleArray = styleArray.map((style) => ({
    'selectors': style.split('{')[0].split(',').map((selector) => selector.trim()),
    'styles': style.split('{')[1].replace('}', '').trim()
  }));

  styleArray.forEach((style) => style.styles = style.styles.split(';'));
  
  styleArray.forEach((style, s) => {
    let stylesValue = {};
    styleArray[s].styles.forEach((styleDeclaration) => {
      if (styleDeclaration !== '') {
        stylesValue[styleDeclaration.split(':')[0].trim()] = styleDeclaration.split(':')[1].trim();
      }
    });
    styleArray[s].styles = stylesValue;
  });
  
  return styleArray;
}  

console.log(convertStylesheetToObject(lastStyleSheet));
<style>
.calendarNavbarLink {
  margin-left: 20px;
}

.today {
  background-color: lawngreen;
}

.weekend {
  background-color: lightsalmon;
}
</style>

Having done this, if you now run:

convertStylesheetToObject(lastStyleSheet)

you will get the following array:

[
  {
    "selectors": [
      ".calendarNavbarLink"
    ],
    "styles": {
      "margin-left": "20px"
    }
  },
  {
    "selectors": [
      ".today"
    ],
    "styles": {
      "background-color": "lawngreen"
    }
  },
  {
    "selectors": [
      ".weekend"
    ],
    "styles": {
      "background-color": "lightsalmon"
    }
  }
]

Step 2:

You now have an array you can parse with a helper function.

E.g. If you want to know

[…] the value of the property background-color of the rule .weekend.

you can run:

getStyleValue('.weekend', 'background-color', styleArray)

which returns the following result:

const getStyleValue = (selector, property, styleArray) => {
  
  let styleValue = 'No value found.';
  
  const selectorFilter = styleArray.filter((element) => element.selectors.includes(selector));
  
  if (selectorFilter.length > 0) {
    const propertyFilter = selectorFilter.filter((element) => Object.keys(element.styles).includes(property));
    if (propertyFilter.length > 0) {
      styleValue = propertyFilter.reverse()[0].styles[property];
    }
  }
  
  return styleValue;
}

let styleArray = [
  {
    "selectors": [
      ".calendarNavbarLink"
    ],
    "styles": {
      "margin-left": "20px"
    }
  },
  {
    "selectors": [
      ".today"
    ],
    "styles": {
      "background-color": "lawngreen"
    }
  },
  {
    "selectors": [
      ".weekend"
    ],
    "styles": {
      "background-color": "lightsalmon"
    }
  }
];

console.log(getStyleValue('.weekend', 'background-color', styleArray));

Just inject a pseudo element in the body,
assign the desired css rules
and analyze its properties.

$(document).ready(function() {
  var $weekend = $("<div></div>").addClass("weekend");
  $weekend.appendTo("body");
  console.log($weekend.css("background-color"));
  $weekend.remove();
});
.calendarNavbarLink {
  margin-left: 20px;
}

.today {
  background-color: lawngreen;
}

.weekend {
  background-color: lightsalmon;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật