This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Box() { | |
return ` | |
<div class="box" onClick="Box.onClick(this)"></div> | |
`; | |
} | |
Box.onClick = (box) => { | |
box.classList.toggle('box_big'); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function NameEditor() { | |
return ` | |
<div class="nameEditor"> | |
<div>Enter you name:</div> | |
<input onKeyUp="NameEditor.onKeyUp(this)" /> | |
<div> | |
Hello, <span></span> | |
</div> | |
</div> | |
`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function NameEditor({ instanceIndex }) { | |
return ` | |
<div class="nameEditor"> | |
<div>Enter you name:</div> | |
<input onKeyUp="NameEditor.onKeyUp(this, ${instanceIndex})" /> | |
<div> | |
Hello, <span id="nameEditor__name-${instanceIndex}"></span> | |
</div> | |
</div> | |
`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function App({ url }) { | |
return ` | |
<!doctype html> | |
<html class="app"> | |
<head> | |
... | |
</head> | |
<body class="app__body"> | |
${NameEditor({ instanceIndex: 0 })} | |
${NameEditor({ instanceIndex: 1 })} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
Hello, <span id="nameEditor__name"></span> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Box({ text }) { | |
return ` | |
<div class="box" onClick="Box.onClick()"> | |
${text} | |
</div> | |
`; | |
} | |
Box.onClick = () => { | |
alert('box was clicked'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function App({ url }) { | |
return ` | |
... | |
${NameEditor({ id: 0 })} | |
${NameEditor({ id: 1 })} | |
${NameEditor({ id: 2 })} | |
... | |
` | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function NameEditor({ id }) { | |
// Add color to distinguish name editors | |
const color = { | |
0: '#b6e1c1', | |
1: '#b7c4ed', | |
}[id]; | |
return ` | |
<div class="nameEditor" style="background: ${color}"> | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Box = () => '<div class="box"></div>'; | |
const boxes = document.getElementsByClassName('box'); | |
document.body.innerHTML = `${Box()}${Box()}${Box()}`; | |
console.log(boxes.length); // 3 | |
document.body.innerHTML = `${Box()}`; | |
console.log(boxes.length); // 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const nameElems = {}; | |
function getNameElems(id) { | |
return nameElems[id] || ( | |
nameElems[id] = document.getElementsByClassName(`nameEditor__name-${id}`) | |
); | |
} | |
NameEditor.onKeyUp = (input, id) => { | |
for (const elem of getNameElems(id)) { |