Categories
JavaScript Answers

How to make input fields read-only through JavaScript?

Sometimes, we want to make input fields read-only through JavaScript.

In this article, we’ll look at how to make input fields read-only through JavaScript.

How to make input fields read-only through JavaScript?

To make input fields read-only through JavaScript, we use the setAttribute method.

For instance, we write

document.getElementById("myReadonlyInput").setAttribute("readonly", "true");

to select the input with getElementById.

Then we call setAttribute with the attribute name and value to set the readonly attribute of the input to true.

Conclusion

To make input fields read-only through JavaScript, we use the setAttribute method.

Categories
JavaScript Answers

How to allow CORS in JavaScript?

Sometimes, we want to allow CORS in JavaScript.

In this article, we’ll look at how to allow CORS in JavaScript.

How to allow CORS in JavaScript?

To allow CORS in JavaScript, we can use the res.header method.

For instance, we write

const app = express();

app.use((req, res, next) => {
  res.header("Access-Control-Allow-Origin", "*");
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept"
  );
  next();
});

to call app.use with a callback that sends the headers to enable CORS.

We send the Access-Control-Allow-Origin and Access-Control-Allow-Headers headers to enable CORS.

Conclusion

To allow CORS in JavaScript, we can use the res.header method.

Categories
JavaScript Answers

How to use Async/Await with Axios in React.js?

Sometimes, we want to use Async/Await with Axios in React.js.

In this article, we’ll look at how to use Async/Await with Axios in React.js.

How to use Async/Await with Axios in React.js?

To use Async/Await with Axios in React.js, we can use it in a function.

For instance, we write

const axios = require("axios").default;

const sendGetRequest = async () => {
  try {
    const resp = await axios.get("https://jsonplaceholder.typicode.com/posts", {
      headers: {
        authorization: "Bearer YOUR_JWT_TOKEN_HERE",
      },
    });

    console.log(resp.data);
  } catch (err) {
    console.error(err);
  }
};

sendGetRequest();

to define the sendGetRequest function.

In it, we call axios.get to make a get request.

And then we get the response body from resp.data.

We get the error from err.

Conclusion

To use Async/Await with Axios in React.js, we can use it in a function.

Categories
JavaScript Answers

How to detect back button or hash change in URL with JavaScript?

Sometimes, we want to detect back button or hash change in URL with JavaScript.

In this article, we’ll look at how to detect back button or hash change in URL with JavaScript.

How to detect back button or hash change in URL with JavaScript?

To detect back button or hash change in URL with JavaScript, we listen to the popstate event.

For instance, we write

window.addEventListener("popstate", (event) => {
  console.log(document.location, event.state);
});

to call addEventListener to listen to the popstate event triggered by window.

In the event handler callback, we get the current location with document.location.

And we get the change made with event.state.

Conclusion

To detect back button or hash change in URL with JavaScript, we listen to the popstate event.

Categories
JavaScript Answers

How to get array elements fulfilling a condition with JavaScript?

Sometimes, we want to get array elements fulfilling a condition with JavaScript.

In this article, we’ll look at how to get array elements fulfilling a condition with JavaScript.

How to get array elements fulfilling a condition with JavaScript?

To get array elements fulfilling a condition with JavaScript, we use the array filter method.

For instance, we write

const filterResult = [1, 16, 4, 6].filter((x) => {
  return x > 5;
});
console.log(filterResult);

to call [1, 16, 4, 6].filter with a function that checks if the item x being iterated through is bigger than 5.

If it’s bigger than 5, then it’ll be included in the returned array.

Conclusion

To get array elements fulfilling a condition with JavaScript, we use the array filter method.