Categories
JavaScript Answers

How to add content to HTML body using JavaScript?

Sometimes, we want to add content to HTML body using JavaScript.

In this article, we’ll look at how to add content to HTML body using JavaScript.

How to add content to HTML body using JavaScript?

To add content to HTML body using JavaScript, we call the insertAdjacentHTML method.

For instance, we write

document
  .getElementById("container")
  .insertAdjacentHTML("beforeend", '<div id="idChild"> content html </div>');

to select the element with getElementByid.

Then we call insertAdjacentHTML to insert HTML as the last child of the element with ID container.

Conclusion

To add content to HTML body using JavaScript, we call the insertAdjacentHTML method.

Categories
JavaScript Answers

How to define regular expression for number with length of 4, 5 or 6 with JavaScript?

Sometimes, we want to define regular expression for number with length of 4, 5 or 6 with JavaScript.

In this article, we’ll look at how to define regular expression for number with length of 4, 5 or 6 with JavaScript.

How to define regular expression for number with length of 4, 5 or 6 with JavaScript?

To define regular expression for number with length of 4, 5 or 6 with JavaScript, we use the \d pattern.

For instance, we write

const re = /\d{4,6}/;

to define the re regex that matches strings with 4 to 6 digits.

\d matches digits.

And {4,6} matches digit groups with length between 4 and 6.

Conclusion

To define regular expression for number with length of 4, 5 or 6 with JavaScript, we use the \d pattern.

Categories
JavaScript Answers

How to add a hash table in JavaScript?

To add a hash table in JavaScript, we create an object literal.

For instance, we write

const myHash = {};
myHash["one"] = [1, 10, 5];
myHash["two"] = [2];
myHash["three"] = [3, 30, 300];

to define the myHash object.

And then add keys and set them to values.

'one', 'two' and 'three' are the keys and the arrays are their values.

Categories
JavaScript Answers

How to add img element onclick event handler with JavaScript?

Sometimes, we want to add img element onclick event handler with JavaScript.

In this article, we’ll look at how to add img element onclick event handler with JavaScript.

How to add img element onclick event handler with JavaScript?

To add img element onclick event handler with JavaScript, we set the onclick attribute to the JavaScript code we want to run.

For instance, we write

<img src="https://picsum.photos/30/30" onclick="window.open(this.src)" />

to set the onclick attribute to window.open(this.src) to call window.open with the img element’s src attribute to open the image in a new window when we click the image.

Conclusion

To add img element onclick event handler with JavaScript, we set the onclick attribute to the JavaScript code we want to run.

Categories
JavaScript Answers

How to count the number of times a same value appears in a JavaScript array?

Sometimes, we want to count the number of times a same value appears in a JavaScript array.

In this article, we’ll look at how to count the number of times a same value appears in a JavaScript array.

How to count the number of times a same value appears in a JavaScript array?

To count the number of times a same value appears in a JavaScript array, we use the array filter method.

For instance, we write

const getOccurrence = (array, value) => {
  return array.filter((v) => v === value).length;
};

const arr = [2, 3, 1, 3, 4, 5, 3, 1];
console.log(getOccurrence(arr, 1));

to define the getOccurrence function with the array and value as parameters.

And we return the number of times value appears in array by calling “array.filterwith a callback that returns the predicatevinarrayequalsvalueand then get the returned array'slength`.

Conclusion

To count the number of times a same value appears in a JavaScript array, we use the array filter method.