Categories
JavaScript Answers

How to stop reloading page with Enter Key with JavaScript?

Sometimes, we want to stop reloading page with Enter Key with JavaScript.

In this article, we’ll look at how to stop reloading page with Enter Key with JavaScript.

How to stop reloading page with Enter Key with JavaScript?

To stop reloading page with Enter Key with JavaScript, we call preventDefault.

For instance, we write

document.getElementById("searchForm").addEventListener(
  "submit",
  (e) => {
    e.preventDefault();
    search(document.getElementById("searchText"));
  },
  false
);

to select the form with getElementById.

Then we call addEventListener to add an event listener for the submit event.

We use a function that calls preventDefault to stop the default submit behavior to stop the page reloading.

And then we call search with the field with the search text we get from getElementById.

Conclusion

To stop reloading page with Enter Key with JavaScript, we call preventDefault.

Categories
JavaScript Answers

How to fix TypeError: Cannot read property ‘style’ of null error with JavaScript?

Sometimes, we want to fix TypeError: Cannot read property ‘style’ of null error with JavaScript.

In this article, we’ll look at how to fix TypeError: Cannot read property ‘style’ of null error with JavaScript.

How to fix TypeError: Cannot read property ‘style’ of null error with JavaScript?

To fix TypeError: Cannot read property ‘style’ of null error with JavaScript, we should make sure the element we’re setting the styles for exists.

For instance, we write

document.getElementById("note").style.display = "block";

to style the display style for the element with ID note.

We should make sure getElementById doesn’t return null.

Conclusion

To fix TypeError: Cannot read property ‘style’ of null error with JavaScript, we should make sure the element we’re setting the styles for exists.

Categories
JavaScript Answers

How to detect browser back button click with JavaScript?

Sometimes, we want to detect browser back button click with JavaScript.

In this article, we’ll look at how to detect browser back button click with JavaScript.

How to detect browser back button click with JavaScript?

To detect browser back button click with JavaScript, we listen the popstate event.

For instance, we write

window.onpopstate = () => {
  alert("clicked back button");
};
history.pushState({}, "");

to set the window.onpopstate property to a function that calls alert to show an alert.

The method is called when we click the back button on the browser.

Conclusion

To detect browser back button click with JavaScript, we listen the popstate event.

Categories
JavaScript Answers

How to rotate and scale an img at the same time with CSS?

Sometimes, we want to rotate and scale an img at the same time with CSS.

In this article, we’ll look at how to rotate and scale an img at the same time with CSS.

How to rotate and scale an img at the same time with CSS?

To rotate and scale an img at the same time with CSS, we use rotate and scale.

For instance, we write

<img class="rotate-img" src="https://picsum.photos/300/300" />

to add an img element.

Then we write

.rotate-img {
  -webkit-transform: rotate(90deg) scale(0.2);
  transform: rotate(90deg) scale(0.2);
  left: -200px;
  position: relative;
}

to select the rotate-img class and set its transform property to add the rotate and scale options.

Conclusion

To rotate and scale an img at the same time with CSS, we use rotate and scale.

Categories
JavaScript Answers

How to pass variables to JavaScript in Express.js?

To pass variables to JavaScript in Express.js, we put a property in the context.

For instance, we write

app.get("/test.js", (req, res) => {
  res.set("Content-Type", "application/javascript");
  res.render("testPage", { myVar: "foo" });
});

to call res.render to render the testPage template.

We call it with an object with the variables we want to pass to the template.

Then we render the variable value with

<script>
  const myVar = <%- JSON.stringify(myVar) %>;
</script>

Conclusion

To pass variables to JavaScript in Express.js, we put a property in the context.