Categories
JavaScript Answers

How to split first name and last name using JavaScript?

Sometimes, we want to split first name and last name using JavaScript.

In this article, we’ll look at how to split first name and last name using JavaScript.

How to split first name and last name using JavaScript?

To split first name and last name using JavaScript, we use the split method.

For instance, we write

const names = "Paul Steve Jones".split(" ");

to call split with ' ' to split the names into an array of name strings.

Conclusion

To split first name and last name using JavaScript, we use the split method.

Categories
JavaScript Answers

How to do fade-in and fade-out with JavaScript and CSS?

Sometimes, we want to do fade-in and fade-out with JavaScript and CSS.

In this article, we’ll look at how to do fade-in and fade-out with JavaScript and CSS.

How to do fade-in and fade-out with JavaScript and CSS?

To do fade-in and fade-out with JavaScript and CSS, we use the transition CSS property.

For instance, we write

<button id="handle">Fade</button>
<div id="slideSource">Whatever you want here - images or text</div>

to add a button and the div we want to element.

Then we write

#slideSource {
  opacity: 1;
  transition: opacity 1s;
}

#slideSource.fade {
  opacity: 0;
}

to add the transition effects with the transition property.

We animate the opacity between 0 and 1.

Then we write

const slideSource = document.getElementById("slideSource");

document.getElementById("handle").onclick = () => {
  slideSource.classList.toggle("fade");
};

to select the div with getElementById.

And then we select the button and set its onclick property to a function that toggles the fade class to animate the opacity between 1 and 0 and vice versa when we click on it.

Conclusion

To do fade-in and fade-out with JavaScript and CSS, we use the transition CSS property.

Categories
JavaScript Answers

How to get path from the request with Node.js and JavaScript?

Sometimes, we want to get path from the request with Node.js and JavaScript.

In this article, we’ll look at how to get path from the request with Node.js and JavaScript.

How to get path from the request with Node.js and JavaScript?

To get path from the request with Node.js and JavaScript, we use the request.url property.

For instance, we write

const http = require("http");
const url = require("url");

const start = () => {
  const onRequest = (request, response) => {
    const pathname = url.parse(request.url).pathname;
    console.log(pathname);
    response.writeHead(200, { "Content-Type": "text/plain" });
    response.write("Hello World");
    response.end();
  };

  http.createServer(onRequest).listen(8888);
  console.log("Server has started.");
};

exports.start = start;

to get the path name of the request URL with url.parse(request.url).pathname.

Conclusion

To get path from the request with Node.js and JavaScript, we use the request.url property.

Categories
JavaScript Answers

How to define a regular expression that allows letters, numbers, and spaces with at least one letter or number with JavaScript?

Sometimes, we want to define a regular expression that allows letters, numbers, and spaces with at least one letter or number with JavaScript.

In this article, we’ll look at how to define a regular expression that allows letters, numbers, and spaces with at least one letter or number with JavaScript.

How to define a regular expression that allows letters, numbers, and spaces with at least one letter or number with JavaScript?

To define a regular expression that allows letters, numbers, and spaces with at least one letter or number with JavaScript, we add all the required patterns in the regex.

For instance, we write

/^[ _]*[A-Z0-9][A-Z0-9 _]*$/;

to add [ _] to check for spacers.

And we use [A-Z0-9][A-Z0-9 _] to check for at least 1 number or letter.

Conclusion

To define a regular expression that allows letters, numbers, and spaces with at least one letter or number with JavaScript, we add all the required patterns in the regex.

Categories
JavaScript Answers

How to change a select value from JavaScript?

Sometimes, we want to change a select value from JavaScript.

In this article, we’ll look at how to change a select value from JavaScript.

How to change a select value from JavaScript?

To change a select value from JavaScript, we set the selectedIndex or value property.

For instance, we write

<select id="select">
  <option value="defaultValue">Default</option>
  <option value="Option1">Option1</option>
  <option value="Option2">Option2</option>
</select>

to add a select element.

Then we write

document.getElementById("select").selectedIndex = 0;
document.getElementById("select").value = "defaultValue";

to select the select element with getElementById.

Then we set the first option as the value by setting selectedIndex to 0 or setting value to 'defaultValue'.

Conclusion

To change a select value from JavaScript, we set the selectedIndex or value property.