r/learnjavascript • • Jan 05 '23

Must-know DOM manipulation methods 🔧

Post image
390 Upvotes

29 comments sorted by

26

u/Driamer Jan 05 '23

Seems that the comments for append & prepend are inverted.

9

u/Low_Mammoth_9371 Jan 05 '23

OMG, (good spot) so sorry, you're right!

4

u/MrTwoSocks Jan 05 '23

I'm just learning js, and was very happy to have spotted that as well

16

u/PositivelyAwful Jan 05 '23 edited Jan 05 '23

I would definitely include

element.insertAdjacentHTML('beforeend', `HTML content`)
element.insertAdjacentText('beforeend', `Text content`)

4

u/Low_Mammoth_9371 Jan 05 '23

Yeah, kind of agree with you. Was toying with the idea of including those but wanted to make it mobile-friendly by limiting the length.

11

u/jkjustjoshing Jan 05 '23

In general, use .textContent over .innerText - https://kellegous.com/j/2013/02/27/innertext-vs-textcontent/

I’ve been a web developer for 10 years and don’t think I’ve ever heard of or used .innerText.

6

u/Low_Mammoth_9371 Jan 05 '23 edited Jan 05 '23

innerText gets and sets text as it appears to a human reader. textContent gets and sets without formatting. Because of this innerText is more performance-heavy but is sometimes the right choice.

Consider this example:--

const address = `

123 Developer Avenue

Only Way is Essex

CB10 1FD

`;

document.body.innerText = address;

// 123 Developer Avenue

// Only Way is Essex

// CB10 1FD

document.body.textContent = address;

// 123 Developer Avenue Only Way is Essex CB10 1FD

--

Though textContent is better performance-wise, in some situations innerText can be the right tool for the job!

I covered more of the differences recently in this YT vid: https://youtu.be/JGy3jUjDTL4

6

u/Rollo_Tomassi_o-O_ Jan 05 '23

I wouldn't recommend INNER HTML because

7

u/Low_Mammoth_9371 Jan 05 '23

I think it's more nuanced than that.

innerHTML will render text as HTML to the DOM. This isn't a problem if you are creating non-dynamic code blocks and inserting them in the DOM or even when appending dynamic data as long as the source is trusted.

However, IF the data you are appending is comes from a non-trivially insecure source or (and this is the big one) includes user input, you should avoid it because someone could then run JS on your page, potentially stealing user data.

10

u/[deleted] Jan 05 '23

It’d be nice if folks started posting these as text snippets rather than images for the benefit of visually-impaired folks

2

u/Low_Mammoth_9371 Jan 05 '23

Cool idea!

I'd be happy to do this but is it possible? Maybe I could post it as text in a comment...

2

u/jack_waugh Jan 06 '23

Not only for visually impaired brothers and sisters, but also for those who would like to paste the code into editors and REPLs and so on.

In the not-fancy-pants editor, i. e. the Markdown editor, insert your code with an extra four spaces at the start of each line. It may be necessary to put a blank line before and after the body of code.

6

u/Foaric Jan 05 '23

Noob question: Isn't this what jQuery is for? Is it needed if this is all that easy in vanilla JavaScript? Or is it more for using fine-grained selectors?

7

u/Low_Mammoth_9371 Jan 05 '23

In my opinion, jQuery hasn't been necessary for some time. Old JS (pre-ES6 upgrade in around 2015) was pretty long-winded. It made a lot of sense to use jQuery before because reduced the amount of code you need to write significantly.

However, now vanilla JS is a lot easier to use. I don't think you need jQuery at all. And you can save yourself importing a library. But it is still good to know a bit of jQuery because it's used quite commonly on older code bases and some still use it writing new ones (though it's dipped in popularity).

This is not to diss jQuery: many of the innovations in vanilla JS have been inspired by features that are popular in jQuery. So we should all say thanks to jQuery in the end, even if it's best to write new code bases without it.

13

u/masticore252 Jan 05 '23

If you are using jQuery only for DOM manipulation, you probably don't need it

jQuery was originally developed when vanilla javascript was not as easy as it is now and also some things were different in different browsers so jQuery abstracted that differences away

Back then if you wanted to make an AJAX request you used XMLHttpRequest and it was a pain to use (you had to lister for events in that object but the event names changed in IE 🤦‍♂️)so a simple AJAX request became a liability, jQuery abstracted that into its $.ajax object making it super easy

You can use selectors in vanilla javascript like you do in jQuery with methods like document.querySelector and document.querySelectorAll

6

u/TorbenKoehn Jan 05 '23

There is no need for jQuery anymore. For a long time already now. It was for a time when most of these APIs didn’t exist.

People installing jQuery today or who are not in the process of weeding it out are committing a great mistake.

jQuery changed the way we wrote JavaScript (their slogan!) and many of the new APIs were designed directly with jQuery in mind (eh document.querySelector)

3

u/senocular Jan 05 '23

Originally jQuery both simplified and normalized what, at the time, was a fairly fractured DOM API, especially given Internet Explorer and its many iterations. As standards started to take hold, consistency was more prevalent and we got new APIs that allowed us to have the same functionality that jQuery provided out of the box (namely querySelectorAll) making jQuery less necessary. For the most part everything you can do in jQuery you can do with vanilla Web APIs, though the jQuery equivalent will probably be a little shorter to write. For comparisons, see: https://youmightnotneedjquery.com/

1

u/manwhowasnthere Jan 05 '23

This was what Jquery was largely for, way back in the day (ie ~10 years ago).

Now most of what made JQ useful is native in the browser, and browsers are far less quirky and unique (like oldass IE versions)

11

u/driftking428 Jan 05 '23

If you're new, memorize these. I practiced a lot of leet code for a job interview then looked like a fool when my DOM manipulation skills weren't developed.

5

u/Unboxed-ii Jan 05 '23

This is not good advice. If you’re new, don’t memorize anything.

1

u/driftking428 Jan 06 '23

Care to elaborate?

0

u/papanastty Jan 24 '26

programming is not meant to be memorized

1

u/deniercounter Jan 05 '23

Unless... well unless you gave up some 10 years ago and had an idea to invent a JavaScript framework just to be able to hold a client state in the browser.

1

u/schm0 Jan 05 '23

Data attributes should be on here!

1

u/jack_waugh Jan 06 '23

For whipping up trees and forests of DOM nodes, I like to use a thin layer over the primitive DOM calls.

1

u/[deleted] Jan 06 '23

Nice man, it took me a whole 3 days to learn.