-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
60 lines (44 loc) · 1.45 KB
/
script.js
File metadata and controls
60 lines (44 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
function addKeyValuePair() {
const keyValueInputs = document.getElementById("keyValueInputs");
const newPairDiv = document.createElement("div");
newPairDiv.className = "key-value-input";
newPairDiv.innerHTML = `
<div class="key-value-input">
<input type="text" placeholder="key" name="key" class="key-input" />
<input type="text" placeholder="value" name="value" class="value-input" />
</div>
`;
keyValueInputs.appendChild(newPairDiv);
}
form.addEventListener("submit", (event) => {
event.preventDefault();
const data = {};
const keyValuePairs = document.querySelectorAll(".key-value-input");
keyValuePairs.forEach((pair) => {
const keyInput = pair.querySelector(".key-input");
const valueInput = pair.querySelector(".value-input");
const key = keyInput.value;
const value = valueInput.value;
data[key] = value;
});
document.getElementById("json").textContent = JSON.stringify(
data,
undefined,
2
);
});
const copyButton = document.getElementById("copyButton");
const jsonContent = document.getElementById("json");
copyButton.addEventListener("click", () => {
const range = document.createRange();
range.selectNode(jsonContent);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
document.execCommand("copy");
selection.removeAllRanges();
copyButton.innerText = "Copied!";
setTimeout(() => {
copyButton.innerText = "Copy";
}, 1000);
});