TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How to manage HTML DOM with vanilla JavaScript only?

1149 pointsby velmuabout 5 years ago

52 comments

davnicwilabout 5 years ago
Great resource, I&#x27;ve bookmarked it.<p>You know where vanilla JS still has a legitimate usecase in the era of SPAs? Landing pages.<p>- Mostly static HTML, time to first render is critical, all you want is a little bit of (progressively added) flair<p>- Can be inlined for instant loading&#x2F;execution, no bundle download (or SSR) necessary<p>- Code has low volume and scope so structure &#x2F; spaghetti isn&#x27;t a concern<p>- You can separate it out from the rest of your SPA bundle(s), so shipping changes is completely decoupled from your SPA CI&#x2F;CD process and can be done as frequently as you like and <i>very</i> quickly
评论 #22766755 未加载
评论 #22764838 未加载
评论 #22764716 未加载
评论 #22764307 未加载
zoom6628about 5 years ago
This site is a real gem. Clean, simple, and minus the excessive complexity and obfuscation caused by frameworks. Doing &amp; Learning things in plain JS we can learn fundamentals.
评论 #22758987 未加载
评论 #22759225 未加载
评论 #22759243 未加载
评论 #22766991 未加载
评论 #22758904 未加载
vbezhenarabout 5 years ago
The problem with vanilla JS is not APIs, API is the easy thing. The problem is building proper architecture, so code won&#x27;t quickly turn into spaghetti. With frameworks like Angular or React, it&#x27;s much easier, as the general structure is dictated by framework and developer just have to follow good practice. With vanilla JS it&#x27;s the wild west.
评论 #22759273 未加载
评论 #22760056 未加载
评论 #22759719 未加载
评论 #22759625 未加载
评论 #22759166 未加载
评论 #22759236 未加载
评论 #22759595 未加载
评论 #22761468 未加载
评论 #22759165 未加载
评论 #22760158 未加载
评论 #22759423 未加载
评论 #22759916 未加载
评论 #22760151 未加载
评论 #22761308 未加载
评论 #22759195 未加载
评论 #22763165 未加载
评论 #22761046 未加载
评论 #22760820 未加载
评论 #22764699 未加载
评论 #22768390 未加载
评论 #22761428 未加载
评论 #22760199 未加载
评论 #22766749 未加载
评论 #22759590 未加载
评论 #22767507 未加载
评论 #22759948 未加载
评论 #22759275 未加载
评论 #22763171 未加载
评论 #22760430 未加载
评论 #22759310 未加载
评论 #22761014 未加载
评论 #22759121 未加载
评论 #22759824 未加载
评论 #22763192 未加载
评论 #22762055 未加载
评论 #22765894 未加载
评论 #22759193 未加载
Leaceabout 5 years ago
Interesting pattern, first time I saw this:<p><pre><code> switch (true) { case cellA &gt; cellB: return 1; case cellA &lt; cellB: return -1; case cellA === cellB: return 0; } </code></pre> Source: <a href="https:&#x2F;&#x2F;htmldom.dev&#x2F;sort-a-table-by-clicking-its-headers" rel="nofollow">https:&#x2F;&#x2F;htmldom.dev&#x2F;sort-a-table-by-clicking-its-headers</a>
评论 #22759230 未加载
评论 #22759351 未加载
评论 #22759198 未加载
评论 #22764418 未加载
评论 #22759219 未加载
评论 #22760172 未加载
drooblesabout 5 years ago
I do all my personal mvps now in vanilla JS, I can get ideas fleshed out faster without fumbling over frameworks.
评论 #22759100 未加载
tabtababout 5 years ago
Making decent UI&#x27;s went from bicycle science to rocket science. VB-classic was easy to learn and easy to use, and you had coordinate control over where most things went on the screen. If your layout engine didn&#x27;t put it where you had planned, you can just tell the widget to &quot;go right here&quot;. You felt in charge of the screen, not some abstract layout layer with an attitude. A manager (person) could draw something on the back of a napkin and you could make it <i>exactly</i> match the napkin because you were in charge of exactly where things went. (Layout engines were optional then.)<p>The newer UI frameworks are round-about and confusing. They make it easy to get 80% of what you want, but that last 20% is bear, requiring long trial-and-error periods, unless you are heavily versed in them. Abstraction is only nice when it does what you want, not when it becomes &quot;abstract art&quot;.<p>The price for &quot;responsive&quot; (client-side auto-placement) has been huge. In applications that are primarily for office desktops, their advantage is lost. Mouse and PC&#x27;s is still where most real work is done. Instead we are using things like Bootstrap, designed for social networks, not real work. Glamour won over logic.<p>It&#x27;s great job security for UI experts, but expensive for organizations because they need stack specialists whereby before a one-man-band (or 2) could make useful apps for less than half the cost and time without having to spend years with UI-Yoda to master some screwy layout engine.<p>Something is wrong.
评论 #22761725 未加载
评论 #22761714 未加载
评论 #22762155 未加载
评论 #22761707 未加载
评论 #22761764 未加载
评论 #22761727 未加载
评论 #22762032 未加载
评论 #22761692 未加载
评论 #22762175 未加载
评论 #22761891 未加载
评论 #22762352 未加载
评论 #22762330 未加载
评论 #22761809 未加载
评论 #22762058 未加载
评论 #22761688 未加载
评论 #22762297 未加载
评论 #22762760 未加载
评论 #22761566 未加载
jancsikaabout 5 years ago
&gt; This approach isn&#x27;t recommended because we can only attach one handler for each event.<p>Except when you <i>do</i> want that behavior. E.g., you only want to set one listener and have a stream of data coming in that can trigger various attachments&#x2F;detachments to the same element(s) over and over.<p>With the ancient &quot;on&quot; methods you get a single method that you can replace at will. Unbinding happens automatically. For example, you can keep assigning to an anonymous function till your heart&#x27;s content and there will only ever be one callback in that slot attached as the author states.<p>With the new, &quot;recommended&quot; methods you must manually track the state and unbind old callbacks. For example, you would have leaky behavior if you kept assigning anonymous functions with addEventListener. And you can&#x27;t easily hack your way out of the state problem because the DOM doesn&#x27;t give you a way to fetch the current number of listeners, reset them to default or really know anything about them.<p>So the recommended behavior does not include in its design the ability to achieve the useful old behavior.<p>But at least DOM does have a convenience method to toggle class that will save a good 10 seconds per year of dev time.
评论 #22760340 未加载
austincheneyabout 5 years ago
Coincidentally, I just commented yesterday about an interview experience I had about the DOM. I did not expect it to be so highly upvoted. <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=22740897" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=22740897</a>
评论 #22759333 未加载
评论 #22759807 未加载
caseymarquisabout 5 years ago
I recently built a small side project in vanilla js for fun. I just didn&#x27;t want to break out webpack and waste an hour getting it set up.<p>I got pretty far, did all the design. I wanted to run a client side simulation in javascript though, and found I needed to host a local server as chrome refused to read modules from a file due to CORS permissions (running chrome with the usual arguments didn&#x27;t fix it).<p>At this point, since I needed an outside piece of software to run things anyway, I figured I might as well switch to webpack dev-server and get the whole modern frontend ecosystem. I then quickly started replacing my home grown dom syncing with vuejs, and snowballed from there.<p>I definitely learned a couple things, so time well spent, but I&#x27;m also going straight to webpack next time. I know it&#x27;ll only take a few hours before I start hitting the limits of what vanilla js can do, and while reinventing the wheel was fun and educational, it didn&#x27;t result in a better wheel.
评论 #22758849 未加载
评论 #22758891 未加载
评论 #22758895 未加载
评论 #22763669 未加载
评论 #22759836 未加载
评论 #22758926 未加载
评论 #22758845 未加载
turniplaabout 5 years ago
Love the website, too bad it doesn&#x27;t use nor mention the new and much improved DOM APIs.<p>This swap function is just `a.replaceWith(c, d, e, …)` in DOM4<p><a href="https:&#x2F;&#x2F;htmldom.dev&#x2F;swap-two-nodes" rel="nofollow">https:&#x2F;&#x2F;htmldom.dev&#x2F;swap-two-nodes</a><p>If you haven&#x27;t seen this before, I highly suggest looking into it.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;WebReflection&#x2F;dom4" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;WebReflection&#x2F;dom4</a> (I can&#x27;t find any articles about DOM 4, so this polyfill’s readme will have to do)
bfred_itabout 5 years ago
I would never recommend copy-pasting this code into your project:<p><a href="https:&#x2F;&#x2F;htmldom.dev&#x2F;copy-text-to-the-clipboard" rel="nofollow">https:&#x2F;&#x2F;htmldom.dev&#x2F;copy-text-to-the-clipboard</a><p>I get &quot;trying to stay vanilla&quot;, but any reasonably-common snippet should either be installed from npm or published there.<p>For this specific issue there&#x27;s a module named almost exactly the same and it restores the previous selection and focus as well: <a href="https:&#x2F;&#x2F;github.com&#x2F;sindresorhus&#x2F;copy-text-to-clipboard&#x2F;blob&#x2F;master&#x2F;index.js" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;sindresorhus&#x2F;copy-text-to-clipboard&#x2F;blob&#x2F;...</a><p>This is the point of published modules: your code isn&#x27;t stuck into the version someone wrote in 10 minutes 5 years ago.
评论 #22761925 未加载
评论 #22761277 未加载
评论 #22762320 未加载
cousin_itabout 5 years ago
Nice collection of recipes! Just a few quibbles about the website design:<p><pre><code> * The back and forward buttons lose scroll position. * Visited links look the same as non-visited. * In each recipe, top 70% of the screen is headers and whitespace, bottom 30% is content (on my laptop).</code></pre>
评论 #22762530 未加载
krossitalkabout 5 years ago
This is a really great set of demos! The resizable table columns really irks me because I spent so long trying to make something similar and it ended up being hundreds of lines longer.
michaelbuckbeeabout 5 years ago
This is nice - it reminds me of the VanillaJS &quot;framework&quot; site - <a href="http:&#x2F;&#x2F;vanilla-js.com&#x2F;" rel="nofollow">http:&#x2F;&#x2F;vanilla-js.com&#x2F;</a>
emilfihlmanabout 5 years ago
Pretty good! My only wish is that the site would be like <a href="http:&#x2F;&#x2F;youmightnotneedjquery.com&#x2F;" rel="nofollow">http:&#x2F;&#x2F;youmightnotneedjquery.com&#x2F;</a>, so that the content would be viewable just by clicking the heading and having new space for it, perhaps in a fixed window, instead of separate pages.
chpmrcabout 5 years ago
Ironically that website is built in React.
jagged-chiselabout 5 years ago
Are there any starter tutorials that would build from vanilla JaveScript, implementing solutions to developer problems, then demonstrating how one might use an existing, well-exercised framework to solve those problems? Every time I go try to learn about a framework from the project web site, they assume a tremendous amount of context.<p>Tangentially, my most recent frustration: I wanted to try a couple frameworks together (they had no overlap in functionality - not like trying to use React and Vue together), and they <i>both</i> came with CLI tools to initialize an empty project and complained when it wasn&#x27;t empty. They both had different opinions about how to structure project directories, and neither explained in an obvious place how else one might get started.
评论 #22762342 未加载
evo_9about 5 years ago
I started to look into going vanilla JS for a side project and ended up going with Svelte instead. Worth checking out.<p><a href="https:&#x2F;&#x2F;svelte.dev&#x2F;blog&#x2F;svelte-3-rethinking-reactivity" rel="nofollow">https:&#x2F;&#x2F;svelte.dev&#x2F;blog&#x2F;svelte-3-rethinking-reactivity</a>
评论 #22760202 未加载
miguelmotaabout 5 years ago
It&#x27;s always worth learning a level lower from what you do, so if you write frontend UIs with React it&#x27;s worth learning how DOM manipulation works with vanilla JS to understand the intricacies and appreciate what the library abstracts for you.<p>If you start writing a site in vanilla JS you&#x27;ll eventually end up writing your own UI library basically reinventing the wheel which is fine as a learning experience and personal projects but maybe not the best choice if you&#x27;re working on a large scale project with many developers since UI libraries and frameworks provide a common set of patterns as convention to follow that makes it easier for everybody to be on the same page.
40fourabout 5 years ago
Framework debates aside, if you’re looking to do something in pure JavaScript this looks like a great resource! Bookmarked :)
stasgavrylovabout 5 years ago
Great resource. I&#x27;m wondering whether the examples are your own or copied from across the web?<p>I just wanted to point out that some of them are a bit outdated, and I belive it&#x27;d be nice to maintain this list as a reference with modern approaches.<p>For example, there&#x27;s no need to use `appendChild`, because DOM today has a more versatile `append` method.<p>And `[].forEach.call(cols, function(col) {...}` would look much better and more declarative as `for (const col of cols) { ... }`.<p>These are just first 2 examples that I noticed.
评论 #22759920 未加载
kennydudeabout 5 years ago
One of the issues here is that the examples don&#x27;t add any accessibility hints at all. We really should be encouraging people to add as many as possible!
Kiroabout 5 years ago
If anything this reminds me why I&#x27;m using React. Having to manually add and remove children, classes or anything dynamic is just a pain.
fourier_modeabout 5 years ago
Slightly unrelated: I am a complete beginner in Javascript, and I was trying it out for a small web-based UI. I was surprised how importing one js into another was non-intuitive. One would need to import all the js&#x27;s into the main HTML. Feel free to correct me if I am abusing it.
jborichevskiyabout 5 years ago
Meta-proposition: why can&#x27;t I tell Google to weigh results from this (or some arbitrary site) higher after I have confirmed its usefulness? Searching through JS docs is a nightmare filled with outdated libraries and scummy SEO articles... so hard to filter through the noise.
评论 #22761663 未加载
Waterluvianabout 5 years ago
Interesting how the list doesn&#x27;t begin with use of querySelector or getElementById.<p>I know these sites are made by experts who aren&#x27;t necessarily thinking about beginners, but a foundation, even a small one, is super valuable.
评论 #22758464 未加载
评论 #22758428 未加载
z3t4about 5 years ago
The trick to manage the DOM with vanilla HTML is to <i>not</i> use HTML! Then abstract code into functions. The advantage with modern JS frameworks is that they make it easier to handle state, and state handling is usually very hard. The trick to state handling in JavaScript is to make use of closures, to prevent the anti-pattern of global state where you don&#x27;t know which functions change what state. The only function allowed to change the state should be function that created the element! This can be achieved with event listeners that listens for messages from the server.
edrocheabout 5 years ago
This is such a wonderful resource, I wish we had it when first building our app, which uses no frameworks or libs. We do use TypeScript to generate it, hard to pass up the benefits and syntax it provides.
ericmcerabout 5 years ago
Js Frameworks are built on top of vanilla JS, so if you build an application of any complexity you will eventually start to abstract functionality until you have your own (probably bad) framework.
baristaGeekabout 5 years ago
Ahh thanks. A lot of times you use these fancy frameworks and don&#x27;t know what&#x27;s going on at a lower level. Understanding this makes you understand the fancy frameworks faster.
nojvekabout 5 years ago
I love this. It doesn&#x27;t matter what framework you use, having to raw dom manipulation is eventually a task for any seasoned frontend engineer. Understanding the dom api is absolutely essential, when performance is paramount. Resizing tables, drag&#x2F;drop, e.t.c any advanced interaction will need control of raw dom. Thanks for writing the little recipes.<p>I&#x27;m not dissing Frameworks. They are a tool that help you with certain kinds of problems. Any crafts(wo)man, should know their tools well.
adrianhelabout 5 years ago
DOM diffing is great for most stuff. When you need anything other than &quot;Replace this with that and don&#x27;t defocus my input fields&quot;, DOM manipulations are great. Drag&#x27;n&#x27;drop is a great example of this.<p>There is a lot of potential for the imperative APIs for this though. I even made a proposal:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;adrianhelvik&#x2F;proposal-move-events" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;adrianhelvik&#x2F;proposal-move-events</a>
Kiroabout 5 years ago
I think everyone should know how the DOM works under the hood but the HN hate for frameworks and love for vanilla JavaScript is just unbelievable. As soon as you venture past small JavaScript sprinkle you will regret using vanilla. It feels like a made-up opinion by people who are actually not developing front-end JavaScript seriously. The DOM API is simply unfit for real web apps without a proper framework on top.
评论 #22759180 未加载
评论 #22758989 未加载
maggitabout 5 years ago
I recently did a write-up of my experience with writing maintainable code with vanilla JS: <a href="https:&#x2F;&#x2F;magnushoff.com&#x2F;blog&#x2F;dependency-free-javascript&#x2F;" rel="nofollow">https:&#x2F;&#x2F;magnushoff.com&#x2F;blog&#x2F;dependency-free-javascript&#x2F;</a><p>I am a huge fan of React for the maintainability of the resulting code, and this was my attempt to regain some of that without the dependency.
akrymskiabout 5 years ago
There&#x27;s a large range of approaches between a heavy framework like React and vanilla DOM manipulations. jQuery is one such popular solution, and I don&#x27;t see why such helpers should be completely avoided. Another (full disclosure: I&#x27;m the author) is: <a href="http:&#x2F;&#x2F;techlayer.com&#x2F;espresso.js&#x2F;" rel="nofollow">http:&#x2F;&#x2F;techlayer.com&#x2F;espresso.js&#x2F;</a>
donohoeabout 5 years ago
I&#x27;d be interested to see if other people have similar go-to vanilla JS? If so, can you share a link here?<p>This one is mine:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;donohoe&#x2F;content&#x2F;blob&#x2F;master&#x2F;posts&#x2F;javascript-returns.md" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;donohoe&#x2F;content&#x2F;blob&#x2F;master&#x2F;posts&#x2F;javascr...</a><p>(I follow basic ES6 syntax but easy to switch as needed)
评论 #22758541 未加载
TheRealPomaxabout 5 years ago
For point 11, checking the bbox can be unreliable. JS got an Intersection Observer API a while back - <a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;API&#x2F;Intersection_Observer_API" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;API&#x2F;Intersectio...</a>
woranlabout 5 years ago
Framework comes and goes. What’s cool today will become obsolete many years later. Vanilla code has less dependencies.
cfvabout 5 years ago
Pretty cool!<p>One thing that would massively improve this is if they were written as singular standalone blocks instead of sparse collections of snippets; I&#x27;d like to be able to grab, say, &quot;make an element draggable&quot; and toss it on my codebase as an isolated behavior.
jnetabout 5 years ago
ele.classList.toggle(&#x27;class-name&#x27;);<p>That seems really useful, and I had no idea it existed.
bin0about 5 years ago
This kind of thing sounds great on personal projects, but doesn&#x27;t scale. Like many abstractions, frameworks&#x27; biggest benefit is (arguably) making it easier for teams to work together.
divbzeroabout 5 years ago
Excellent. Reminds me of You Might Not Need jQuery [1] from awhile back.<p>[1]: <a href="http:&#x2F;&#x2F;youmightnotneedjquery.com&#x2F;" rel="nofollow">http:&#x2F;&#x2F;youmightnotneedjquery.com&#x2F;</a>
评论 #22760793 未加载
mattlondonabout 5 years ago
Similar site from much longer ago, but with less content:<p><a href="http:&#x2F;&#x2F;standardjavascript.info&#x2F;" rel="nofollow">http:&#x2F;&#x2F;standardjavascript.info&#x2F;</a>
stevedekorteabout 5 years ago
This is a great resource. I&#x27;ve had to dig through a lot of StackOverflow threads to figure these out. Great to have lots of current answers in one place.
wilsonbrightabout 5 years ago
Great resource. Well built and it is going to helpful to many.
henriquezabout 5 years ago
Nice work. I was looking for something just like this!
niyazpkabout 5 years ago
&gt;&gt; &quot;for modern browsers and IE 11+&quot;<p>Ouch!
评论 #22766060 未加载
redis_mlcabout 5 years ago
I&#x27;m a vanilla-js guy.<p>That cookbook is pretty good.<p>If anybody has a good way of doing a password dialog in vanilla-js, please post a link.<p>It&#x27;s the one thing I&#x27;ve encountered where I wish I had used a framework.
评论 #22758727 未加载
评论 #22759934 未加载
评论 #22758409 未加载
评论 #22758426 未加载
hk__2about 5 years ago
Ironically, the site itself uses ~100ko of gzip’d JS.
评论 #22759005 未加载
guggleabout 5 years ago
That&#x27;s great ! Very useful.
baybal2about 5 years ago
Protip: <a href="https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;API&#x2F;XSLTProcessor" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;API&#x2F;XSLTProcess...</a>
KaoruAoiShihoabout 5 years ago
This is useful for some devs in some situations, but let&#x27;s not go overboard. Teaching newbies to venerate vanilla is almost educational malpractice.
评论 #22758671 未加载
评论 #22758635 未加载
评论 #22758685 未加载
评论 #22759062 未加载
评论 #22758934 未加载