Ahmad Awais Profile picture
Sep 27, 2021 12 tweets 4 min read Read on X
Crash Course on the .classList() Web API.

Thread 🧵👇
👋Howdy, it's Awais @MrAhmadAwais — Head of Developer Relations at RapidAPI. Google Devs Expert, GitHub Star, DO Navigator.

I've coded 100s of open-source software used by millions of devs. Contributed to NASA’s Mars Helicopter mission. Learn more → Awais.dev/about

👇
If you write code for the Web, there are a large number of Web APIs available.

Web APIs are typically used with JavaScript.

One such API is the .classList() API.

Let's learn how to use it well. 👇
The .classList() API 🧠

The Element.classList is a read-only property that returns a live DOMTokenList collection of the class attributes of the element.

It can be used to manipulate the class list. Image
The .classList() API 🧠

👨‍💻 Syntax:

// HTML.
<div class="cover"></div>

// JavaScript.
const div = document.querySelector(".cover");

You can access the classList like this:
div.classList;

Returns a DOMTokenList. It's 0th indexed with JS Array objects. Always case-sensitive. Image
ADD with the .classList() API 🧠

// Add a class.
div.classList.add("colors");

// Add more than one classes.
div.classList.add("colors", "size");

// Spread works!
const classes = ["size", "colors"];
div.classList.add(...classes); Image
REMOVE with the .classList() API 🧠

// Remove a class.
div.classList.remove("colors");

// Remove many classes.
div.classList.remove("colors", "size");

// Spread works!
const classes = ["size", "colors"];
div.classList.remove(...classes); Image
TOGGLE with the .classList() API 🧠

// Toggle a class.
div.classList.toggle("colors");

If the "colors" class is present, remove it, if it's not there, add it. Image
REPLACE with the .classList() API 🧠

// Replace a class.
div.classList.replace("colors", "purple");

This replaces the class "colors" with class "purple". Image
CHECK with the .classList() API 🧠

// Check if the element has a class.
div.classList.contains("colors");

It returns true or false. Image
VIEW with the .classList() API 🧠

// View individual classes.
// For this HTML <div class="colors size">
div.classList.item(0); // colors
div.classList.item(1); // size
div.classList.item(2); // null
div.classList[1]; // colors Image
Liked this thread? Do this ↓

1️⃣ Retweet the first tweet:


2️⃣ Follow me for more @MrAhmadAwais — NTMY!

3️⃣ Reply to share feedback on would you like me to post more such threads on Web Development and other #OneDevMinute tips & tricks?

Peace! ✌️

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Ahmad Awais

Ahmad Awais Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @MrAhmadAwais

Jan 31, 2023
Generally, here are 9 HTTP request methods:

❯ GET
❯ PUT
❯ POST
❯ HEAD
❯ TRACE
❯ PATCH
❯ DELETE
❯ OPTIONS
❯ CONNECT

Did you know? Let's discuss each of these 🧵👇
HTTP defines a set of request methods.

Each request method indicates a different action.

Sometimes we call them HTTP verbs.

Each of them implements a different logic.
📮 GET

Use the GET method to get data from a server.

The GET method is the most common. You send a GET request when you browse a website.

❯❯ Requests using GET should only retrieve data.
Read 12 tweets
Dec 13, 2022
API security best practices 👇🧵 Rapid API Comic: API Securi...
{ 2 / 6 } 1. Authentication. Authenti...
{ 3 / 6 } 2. Validate input. Input va...
Read 7 tweets
Nov 8, 2022
Learn almost everything about HTTP Cache-Control. 👇🧵
✨ Cache-Control

The Cache-Control HTTP header field holds directives (instructions) — in both requests and responses — that control caching in browsers and shared caches (e.g., Proxies, CDNs).

It's all about request and response.

And about caching, of course.
🤖 Syntax of Cache-Control

Caching directives follow the validation rules below:

🔹 Caching directives are case-insensitive.
🔹 However, lowercase is recommended.
🔹 Multiple directives are comma-separated.
🔹 Some directives have an optional argument.
Read 40 tweets
Nov 6, 2022
Learn Cache-Control and how APIs are cached.👇

{ 1 / 6 } RapidAPI Comic: How API requests are cached.
{ 2 / 6 } What is cache? Caching means storing responses in cache memo
{ 3 / 6 } How does caching work? Cache is controlled by the HTTP Cache
Read 7 tweets
Jul 2, 2022
Generally, here are 9 HTTP request methods:

❯ GET
❯ PUT
❯ POST
❯ HEAD
❯ TRACE
❯ PATCH
❯ DELETE
❯ OPTIONS
❯ CONNECT

Did you know? Let's discuss each of these 🧵👇
HTTP defines a set of request methods.

Each request method indicates a different action.

Sometimes we call them HTTP verbs.

Each of them implements a different logic.
📮 GET

Use the GET method to get data from a server.

The GET method is the most common. You send a GET request when you browse a website.

❯❯ Requests using GET should only retrieve data.
Read 12 tweets
Jun 23, 2022
400 FREE API Developer Guides, Comics, & Video Courses. 🧵👇
1️⃣ RapidAPI Learn: RapidAPI.com/learn

API Development using fun challenges (with solutions!) and interactive examples. 8 learn labs.

✨ I promise this is the best way to learn APIs. My team and I put a lot of thought into this. Check it out.
2️⃣ RapidAPI Guides → RapidAPI.com/guides

Short and long-form API Development guides with interactive examples. 357 Guides.

✨ Check out the `Interactive` category — you'll be blown away by the amazing teaching experience.
Read 7 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(