Is there a way to extract specific inline CSS styles from an element, capturing both the style name and its value?
Consider an element with various inline styles like this:
<div style="background-color:red; display:block; background-size:cover; background-attachment:fixed; background-repeat:repeat-y; cursor:pointer; width:100%"></div>
My goal is to retrieve the element's styles (including both the style name and value), focusing only on those related to "background" while disregarding others like "display, cursor, width," and so forth.
To achieve this using jQuery, one might execute the following code snippet:
$("div").attr("style");
This approach returns all of the element's styles, including undesired ones. Ideally, I am seeking a solution that delivers output similar to the following, filtering out non-"background" related styles:
background-color:red; background-size:cover; background-attachment:fixed; background-repeat:repeat-y;
While it is possible to access individual styles like so:
$("div").css("background");
$("div").css("background-size");
The limitation lies in obtaining just the style values without discerning between different variations such as "background-image" or "background-repeat-x/y."