I have been trying to learn how to chain methods in vanilla JS. Following simple tutorials I can add(), subtract() etc. However, going solo and trying to chain DOM elements, I am struggling with.
In the following snippet nothing is outputted, which I believe is due to this.textContent line. How do I get the getId bit?
'use strict'
let chain = {
getId(id) {
document.getElementById(id);
return this;
},
text(content) {
this.textContent = content;
return this;
}
}
window.addEventListener('load', function() {
let dummy = chain.getId('dummy').text('works');
})
<div id='dummy'></div>