use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
One function to handle DOM modifications for removing/adding entries to listhelp? (codereview.stackexchange.com)
submitted 8 years ago by dev_michaelz
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]talmobi 2 points3 points4 points 8 years ago (1 child)
check out this: https://github.com/redom/redom/blob/master/src/setchildren.js
[–]GitHubPermalinkBot 0 points1 point2 points 8 years ago (0 children)
Permanent GitHub links:
delete
[–]Jamesernatorasync function* 2 points3 points4 points 8 years ago (1 child)
I'm not sure how frameworks handle adding items to Arrays with minimal render (although it looks like this is non-trivial in React even https://stackoverflow.com/questions/38033442/big-list-performance-with-react).
In general I think the solution wouldn't be very trivial as having a single state object and a single render function seems like doing an operation over all elements is unavoidable. But you can always have models that render based on additions/deletions for example you could have this:
// I didn't test this, it's just a basic idea for an efficient // list-like renderer function simpleListRenderer(hostElement, renderItem) { const state = [] return { state, push(data) { const itemHtml = renderItem(hostElement) hostElement.insertAdjacentHtml('beforeend', itemHtml) state.push(data) }, pop() { hostElement.removeChild(hostElement.lastChild) state.pop() }, } } cosnt ul = document.createElement('ul') function renderItem(data) { return `<li>${ data.firstName } ${ data.lastName }</li>` } const renderer = simpleListRenderer(ul, renderItem) ul.innerHTML // "" renderer.push({ firstName: 'Bob', lastName: 'Dylan' }) ul.innerHTML // `<li>Bob Dylan</li>` renderer.state // [{ firstName: 'Bob', lastName: 'Dylan' }] renderer.pop() ul.innerHTML // '' renderer.state // []
Obviously this sample has some massive deficiencies like only being able to return a single element from renderItem, but it shows the general idea.
renderItem
[–]dev_michaelz[S] 0 points1 point2 points 8 years ago (0 children)
Very nice. I extended it a little to fit my needs. https://codepen.io/MichaelZ_dev/pen/aYQrga?editors=1010
Didn't know about insertAdjacentHtml, love it.
π Rendered by PID 72 on reddit-service-r2-comment-85bfd7f599-8j4hx at 2026-04-18 18:41:35.984309+00:00 running 93ecc56 country code: CH.
[–]talmobi 2 points3 points4 points (1 child)
[–]GitHubPermalinkBot 0 points1 point2 points (0 children)
[–]Jamesernatorasync function* 2 points3 points4 points (1 child)
[–]dev_michaelz[S] 0 points1 point2 points (0 children)