Categories
JavaScript Answers

How to open window and specific size on click with JavaScript?

Sometimes, we want to open window and specific size on click with JavaScript.

in this article, we’ll look at how to open window and specific size on click with JavaScript.

How to open window and specific size on click with JavaScript?

To open window and specific size on click with JavaScript, we call window.open with a string that specifies the window size.

For instance, we write

window.open("http://example.com", "mywin", "width=500,height=500");

to call window.open to open http://example.com in a window that’s 500px wide by 500px high as specified with

"width=500,height=500"

Conclusion

To open window and specific size on click with JavaScript, we call window.open with a string that specifies the window size.

Categories
JavaScript Answers

How to get old value with onchange() event in text box with JavaScript?

Sometimes, we want to get old value with onchange() event in text box with JavaScript.

In this article, we’ll look at how to get old value with onchange() event in text box with JavaScript.

How to get old value with onchange() event in text box with JavaScript?

To get old value with onchange() event in text box with JavaScript, we can get the old value when the click event is emitted.

For instance, we add an input with

<input type="text" id="test" value="ABS" oldvalue="" />

Then we write

const input = document.querySelector("input");
input.onclick = (e) => {
  e.target.setAttribute("oldvalue", e.target.vakue);
};

input.onchange = (e) => {
  e.target.setAttribute("value", e.target.getAttribute("oldvalue"));
};

to select the input with querySelector.

Then we set input.onclick to a function that gets the input value when we click it.

We get the old input value with e.target.value.

Next we set input.onchange to a function that calls e.target.setAttribute to set the value attribute to the oldvalue attribute’s value we get with getAttribute.

Conclusion

To get old value with onchange() event in text box with JavaScript, we can get the old value when the click event is emitted.

Categories
JavaScript Answers

How to sort arrays numerically by object property value with JavaScript?

Sometimes, we want to sort arrays numerically by object property value with JavaScript.

In this article, we’ll look at how to sort arrays numerically by object property value with JavaScript.

How to sort arrays numerically by object property value with JavaScript?

To sort arrays numerically by object property value with JavaScript, we can use the array sort method.

For instance, we write

const sorted = myArray.sort((a, b) => a.distance - b.distance);

to call myArray.sort with a callback that returns a.distance - b.distance to return a new array that sort the entries in myArray by the value of the distance property of each object in ascending order.

Conclusion

To sort arrays numerically by object property value with JavaScript, we can use the array sort method.

Categories
JavaScript Answers

How to declare an empty two-dimensional array in JavaScript?

Sometimes, we want to declare an empty two-dimensional array in JavaScript.

In this article, we’ll look at how to declare an empty two-dimensional array in JavaScript.

How to declare an empty two-dimensional array in JavaScript?

To declare an empty two-dimensional array in JavaScript, we can use the array fill method.

For instance, we write

const matrix = new Array(5).fill(0).map(() => new Array(4).fill(0));

to call Array with 5 to create an array with 5 empty slots.

Then we call fill to fill the slots with undefined.

Next we call map with a callback to return an array with 4 empty slots and fill them with 0 with fill.

Conclusion

To declare an empty two-dimensional array in JavaScript, we can use the array fill method.

Categories
JavaScript Answers

How to generate array of random dates within two dates with JavaScript?

Sometimes, we want to generate array of random dates within two dates with JavaScript.

In this article, we’ll look at how to generate array of random dates within two dates with JavaScript.

How to generate array of random dates within two dates with JavaScript?

To generate array of random dates within two dates with JavaScript, we can use the Math.random method.

For instance, we write

const randomDate = (start, end) => {
  return new Date(
    start.getTime() + Math.random() * (end.getTime() - start.getTime())
  );
};

const d = randomDate(new Date(2012, 0, 1), new Date());
console.log(d);

to define the randomDate function that takes the start and end dates.

We get the timestamp in between start and end with

start.getTime() + Math.random() * (end.getTime() - start.getTime())

Math.random returns a number between 0 and 1, so we multiply the returned number with (end.getTime() - start.getTime()) and add start.getTime to get a timestamp in milliseconds between start and end and use that as the argument of Date to get a new Date object with the timestamp.

Conclusion

To generate array of random dates within two dates with JavaScript, we can use the Math.random method.