Categories
JavaScript Answers

How to sum of input values in sections with JavaScript?

Sometimes, we want to sum of input values in sections with JavaScript.

In this article, we’ll look at how to sum of input values in sections with JavaScript.

How to sum of input values in sections with JavaScript?

To sum of input values in sections with JavaScript, we can convert the node list of inputs to an array.

Then we can use the array map and reduce methods to add the values together.

For instance, we write:

<form>
  <input type="number" value="2" />
  <input type="number" value="0" />
  <input type="number" value="1" />
  <input type="number" value="3" />
  <input type='submit'>
</form>

to add a form.

Then we select the form and input elements with querySelector and querySelectorAll.

Then we set the form.onsubmit property to the form submit handler.

In it, we call e.preventDefault to stop the default submission behavior.

Then we spread the inputs into an array.

Next, we call map with a callback to get the input value and convert them to numbers.

Finally, we call reduce with a callback to add all the numbers together.

Therefore, sum is 6 according to the console log.

Conclusion

To sum of input values in sections with JavaScript, we can convert the node list of inputs to an array.

Then we can use the array map and reduce methods to add the values together.

Categories
JavaScript Answers

How to duplicate a div onclick event with JavaScript?

Sometimes, we want to duplicate a div onclick event with JavaScript.

In this article, we’ll look at how to duplicate a div onclick event with JavaScript.

How to duplicate a div onclick event with JavaScript?

To duplicate a div onclick event with JavaScript, we can call cloneNode` to clone an element.

Then we can set the onclick property of the cloned element to the same event handler function as the original element.

For instance, we write:

<div id="duplicater">
  foo
</div>

to add a div.

Then we clone it by writing:

let i = 1
const original = document.getElementById('duplicater');
const onClick = () => console.log('clicked')
original.onclick = onClick

const clone = original.cloneNode(true);
i++
clone.id = `duplicater${i}`;
clone.onclick = onClick;
original.parentNode.appendChild(clone);

We select the original element and add a click listener to it with:

const original = document.getElementById('duplicater');
const onClick = () => console.log('clicked')
original.onclick = onClick

Then we deep clone the element by calling cloneNode with true.

Next, we set the ID of the cloned element by setting the id property.

And then we add the click handler by setting clone.onclick to onClick.

Finally, we append it it to the original element’s parent node with original.parentNode.appendChild(clone);.

Conclusion

To duplicate a div onclick event with JavaScript, we can call cloneNode` to clone an element.

Then we can set the onclick property of the cloned element to the same event handler function as the original element.

Categories
JavaScript Answers

How to detect supported video formats for the HTML5 video tag with JavaScript?

Sometimes, we want to detect supported video formats for the HTML5 video tag with JavaScript.

In this article, we’ll look at how to detect supported video formats for the HTML5 video tag with JavaScript.

How to detect supported video formats for the HTML5 video tag with JavaScript?

To detect supported video formats for the HTML5 video tag with JavaScript, we can use the video element’s canPlayType method.

For instance, we write:

<video></video>

to add a video element.

Then we write:

const testEl = document.querySelector('video')
const mpeg4 = "" !== testEl.canPlayType('video/mp4; codecs="mp4v.20.8"');
const h264 = "" !== (testEl.canPlayType('video/mp4; codecs="avc1.42E01E"') ||
  testEl.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"'));
const ogg = "" !== testEl.canPlayType('video/ogg; codecs="theora"');
const webm = "" !== testEl.canPlayType('video/webm; codecs="vp8, vorbis"');
console.log(mpeg4)
console.log(h264)
console.log(ogg)
console.log(webm)

to select the video element with querySelector.

Then we call testEl.canPlayType with various format strings to check if various video formats can be played.

If they can be played, then canPlayType should return something other than an empty string.

Conclusion

To detect supported video formats for the HTML5 video tag with JavaScript, we can use the video element’s canPlayType method.

Categories
JavaScript Answers

How to get hex integer from a string in JavaScript?

Sometimes, we want to get hex integer from a string in JavaScript.

In this article, we’ll look at how to get hex integer from a string in JavaScript.

How to get hex integer from a string in JavaScript?

To get hex integer from a string in JavaScript, we can use the parseInt function.

For instance, we write:

const str = "#FFFFFF"
const hex = parseInt(str.replace(/^#/, ''), 16);
console.log(hex)

We call str.replace to remove the # sign with an empty string.

Then we call parseInt to convert the hex number to a decimal number.

As a result, we get 16777215 for the value of hex.

Conclusion

To get hex integer from a string in JavaScript, we can use the parseInt function.

Categories
JavaScript Answers

How to set an image’s src attribute to another image URL with JavaScript?

Sometimes, we want to set an image’s src attribute to another image URL with JavaScript.

In this article, we’ll look at how to set an image’s src attribute to another image URL with JavaScript.

How to set an image’s src attribute to another image URL with JavaScript?

To set an image’s src attribute to another image URL with JavaScript, we can set the src property of the img element.

For instance, we write:

<img src='https://picsum.photos/50/50'>

to add an img element.

Then we write:

const img = document.querySelector('img')
img.src = 'https://picsum.photos/500/500'

to select the img element with document.querySelector.

And then we set the img.src property with the new image URL.

Now the new image is displayed on screen.

Conclusion

To set an image’s src attribute to another image URL with JavaScript, we can set the src property of the img element.