From b207ebc277b6b0590b720bc6807c3eab24908960 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 29 Dec 2017 00:08:17 +0200 Subject: [PATCH] Added getStyle/setStyle snippets --- snippets/getStyle.md | 13 +++++++++++++ snippets/setStyle.md | 13 +++++++++++++ tag_database | 2 ++ 3 files changed, 28 insertions(+) create mode 100644 snippets/getStyle.md create mode 100644 snippets/setStyle.md diff --git a/snippets/getStyle.md b/snippets/getStyle.md new file mode 100644 index 000000000..40b744a2b --- /dev/null +++ b/snippets/getStyle.md @@ -0,0 +1,13 @@ +### getStyle + +Returns the value of a CSS rule for the specified element. + +Use `Window.getComputedStyle()` to get the value of the CSS rule for the specified element. + +```js +const getStyle = (el, ruleName) => getComputedStyle(el)[ruleName]; +``` + +```js +getStyle(document.querySelector('p'), 'font-size') // '16px' +``` diff --git a/snippets/setStyle.md b/snippets/setStyle.md new file mode 100644 index 000000000..7ac9460af --- /dev/null +++ b/snippets/setStyle.md @@ -0,0 +1,13 @@ +### setStyle + +Sets the value of a CSS rule for the specified element. + +Use `element.style` to set the value of the CSS rule for the specified element to `value`. + +```js +const setStyle = (el, ruleName, value) => el.style[ruleName] = value; +``` + +```js +setStyle(document.querySelector('p'), 'font-size', '20px') // The first

element on the page will have a font-size of 20px +``` diff --git a/tag_database b/tag_database index cf5a1a5b9..1ee03d448 100644 --- a/tag_database +++ b/tag_database @@ -50,6 +50,7 @@ functionName:function gcd:math getDaysDiffBetweenDates:date getScrollPosition:browser +getStyle:browser getType:utility getURLParameters:browser groupBy:array @@ -111,6 +112,7 @@ sample:array scrollToTop:browser sdbm:utility select:object +setStyle:browser shallowClone:object shuffle:array similarity:array