Skip to content

Instantly share code, notes, and snippets.

@n00nietzsche
Created March 8, 2023 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n00nietzsche/6195331c48dbdaccdc10e3563ee60b52 to your computer and use it in GitHub Desktop.
Save n00nietzsche/6195331c48dbdaccdc10e3563ee60b52 to your computer and use it in GitHub Desktop.
템플릿 태그 함수 예제
const insertHTML =
(selector, { position = "beforeend", prefix = "", suffix = "" }) =>
(strings, ...values) => {
const $selected = document.querySelector(selector);
if ($selected) {
const htmlString = strings.reduce((result, string, i) => {
result += string;
if (values[i]) {
result += prefix + values[i] + suffix;
}
return result;
}, "");
$selected.insertAdjacentHTML(position, htmlString);
return;
}
console.warn(
`selector: ${selector} 에 해당하는 엘리먼트가 존재하지 않습니다.`
);
return;
};
const content = "myButton";
insertHTML("#elemId", { prefix: "(", suffix: ")" })`
<button>${content}</button>
`;
@n00nietzsche
Copy link
Author

n00nietzsche commented Mar 8, 2023

결과로

#elemId 아이디를 가진 DOM 요소에

<button>(myButton)</button>

를 붙일 수 있다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment