Categories
JavaScript Answers

How to use querySelectorAll to retrieve direct children with JavaScript?

Sometimes, we want to use querySelectorAll to retrieve direct children with JavaScript.

In this article, we’ll look at how to use querySelectorAll to retrieve direct children with JavaScript.

How to use querySelectorAll to retrieve direct children with JavaScript?

To use querySelectorAll to retrieve direct children with JavaScript, we can use the :scope > pseudoselector.

For instance, we write

const myDiv = document.getElementById("myDiv");
myDiv.querySelectorAll(":scope > .foo");

to get the myDiv element with getElementById.

Then we call querySelectorAll with the ":scope > .foo" select to get the direct children of the myDiv element with the foo class.

Conclusion

To use querySelectorAll to retrieve direct children with JavaScript, we can use the :scope > pseudoselector.

Categories
JavaScript Answers

How to use an authorization header with fetch in React Native?

Sometimes, we want to use an authorization header with fetch in React Native.

In this article, we’ll look at how to use an authorization header with fetch in React Native.

How to use an authorization header with fetch in React Native?

To use an authorization header with fetch in React Native, we set the headers option when we call fetch.

For instance, we write

fetch(url, {
  method: "post",
  headers: new Headers({
    Authorization: "Basic " + btoa("username:password"),
    "Content-Type": "application/x-www-form-urlencoded",
  }),
  body: "foo=1&bar=2",
});

to call fetch with an object with some request options to make a request.

In the object we set the request method to 'post'.

And we set the headers to a Headers object to set the request headers.

In the object we pass into Headres, we set the Authorization header by setting the Authorization property.

And we set the Content-Type header the same way.

We set the body to the request body.

Conclusion

To use an authorization header with fetch in React Native, we set the headers option when we call fetch.

Categories
JavaScript Answers

How to access parent iframe from JavaScript?

Sometimes, we want to access parent iframe from JavaScript.

In this article, we’ll look at how to access parent iframe from JavaScript.

How to access parent iframe from JavaScript?

To access parent iframe from JavaScript, we get the parent document and then use that to get the parent iframe.

For instance, we write

const arrFrames = parent.document.getElementsByTagName("IFRAME");

for (const arrFrame of arrFrames) {
  if (arrFrame === window) {
    console.log("parent frame found");
  }
}

to loop through the iframes returned by getElementsByTagName from the parent document.

And then we loop through the frames with a for-of loop.

In it, we check if arrFrame equals window.

If it is, then we found the parent frame.

Conclusion

To access parent iframe from JavaScript, we get the parent document and then use that to get the parent iframe.

Categories
JavaScript Answers

How to fix no not found route with React-Router v6?

Sometimes, we want to fix no not found route with React-Router v6.

In this article, we’ll look at how to fix no not found route with React-Router v6.

How to fix no not found route with React-Router v6?

To fix no not found route with React-Router v6, we can add a route and then add a catch-all route that redirects to it.

For instance, we write

import { Routes, Route, Navigate } from "react-router-dom";

function App() {
  return (
    <div>
      <Routes>
        <Route path="/404" element={<div>Not found</div>} />
        <Route path="*" element={<Navigate replace to="/404" />} />
      </Routes>
    </div>
  );
}

to create the App component that add 2 Routes.

The first route has path ‘/404’` and renders ‘Not found’.

The 2nd Route is a catch all route that uses the Navigate component to navigate to the /404 route.

We use '*' to match anything that don’t match the paths listed in Routes above it.

Conclusion

To fix no not found route with React-Router v6, we can add a route and then add a catch-all route that redirects to it.

Categories
JavaScript Answers

How to convert ‘1’ to ‘0001’ in JavaScript?

Sometimes, we want to convert ‘1’ to ‘0001’ in JavaScript.

In this article, we’ll look at how to convert ‘1’ to ‘0001’ in JavaScript.

How to convert ‘1’ to ‘0001’ in JavaScript?

To convert ‘1’ to ‘0001’ in JavaScript, we can use the string padStart method.

For instance, we write

const str = "1";
const ans = str.padStart(4, '0');

to call str.padStart with 4 and '0' to prepend '0'‘s to the str string into it reaches length 4.

Therefore, ans is '0001'.

Conclusion

To convert ‘1’ to ‘0001’ in JavaScript, we can use the string padStart method.