Categories
JavaScript Answers

How to show div when radio button selected with JavaScript?

Sometimes, we want to show div when radio button selected with JavaScript.

In this article, we’ll look at how to show div when radio button selected with JavaScript.

How to show div when radio button selected with JavaScript?

To show div when radio button selected with JavaScript, we can listen to the change event of the radio buttons.

For instance, we write:

<input type='radio' name='foo'>
<input type='radio' name='foo'>
<input type='radio' name='foo'>
<div style='display: none'>
  hello
</div>

to add radio buttons and a div that shows when we click on any radio button.

Then we write:

const input = document.querySelectorAll('input')
const div = document.querySelector('div')
for (const i of input) {
  i.onchange = () => {
    div.style.display = 'block'
  }
}

to select all the radio inputs and the div with querySelectorAll and querySelector.

Next, we loop through each input and set the onchange property to a function that’s called when the radio button checked value is changed.

In the function, we set the display CSS property to block to show the div.

Conclusion

To show div when radio button selected with JavaScript, we can listen to the change event of the radio buttons.

Categories
JavaScript Answers

How to split the URL to get URL path in with JavaScript?

Sometimes, we want to split the URL to get URL path in with JavaScript.

In this article, we’ll look at how to split the URL to get URL path in with JavaScript.

How to split the URL to get URL path in with JavaScript?

To split the URL to get URL path in with JavaScript, we can create a URL instance from the URL string.

Then we can use the pathname property to get the URL path.

For instance, we write:

const url = 'http://www.example.com/foo/path2/path3/path4';
const {
  pathname
} = new URL(url);
console.log(pathname);

We create a URL instance from url.

Then we get the pathname from the pathname property.

Therefore, pathname is '/foo/path2/path3/path4'.

Conclusion

To split the URL to get URL path in with JavaScript, we can create a URL instance from the URL string.

Then we can use the pathname property to get the URL path.

Categories
JavaScript Answers

How to save a PNG in a JavaScript string to disk?

Sometimes, we want to save a PNG in a JavaScript string to disk.

In this article, we’ll look at how to save a PNG in a JavaScript string to disk.

How to save a PNG in a JavaScript string to disk?

To save a PNG in a JavaScript string to disk, we can create an anchor element, set the href property to the base64 PNG image string, then call click to download the image.

For instance, we write:

const download = document.createElement('a');
download.href = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
download.download = 'reddot.png';
download.click();

to call document.createElement to create an anchor element.

Then we set download.href to the base64 PNG image string.

Next, we set download.download to the file name we want to save the file as.

Finally, we call click to download the file.

Conclusion

To save a PNG in a JavaScript string to disk, we can create an anchor element, set the href property to the base64 PNG image string, then call click to download the image.

Categories
JavaScript Answers

How to access nested child elements with jQuery?

Sometimes, we want to access nested child elements with jQuery.

In this article, we’ll look at how to access nested child elements with jQuery.

How to access nested child elements with jQuery?

To access nested child elements with jQuery, we can use the child element selector.

For instance, we write:

<div id="ParentDiv">
  <div id="SubDiv1"></div>
  <div id="SubDiv2">
    <input>
  </div>
</div>

to add some divs with an input inside.

Then we write:

const el = $('#ParentDiv input');
console.log(el)

to select the input which is in the div with ID ParentDiv.

As a result, el should be the input which is in the div with ID ParentDiv.

Conclusion

To access nested child elements with jQuery, we can use the child element selector.

Categories
JavaScript Answers

How to select 5 random elements with JavaScript?

Sometimes, we want to select 5 random elements with JavaScript.

In this article, we’ll look at how to select 5 random elements with JavaScript.

How to select 5 random elements with JavaScript?

To select 5 random elements with JavaScript, we can spread the elements into an array.

Then we can use the array sort method to shuffle the array.

And we use slice to pick the first 5 items.

For instance, we write:

<ul>
  <li>First</li>
  <li>Second</li>
  <li>Third</li>
  <li>4th</li>
  <li>6th</li>
  <li>7th</li>
  <li>8th</li>
  <li>9th</li>
  <li>10th</li> 
</ul>

to add some li elements.

Then we write:

const els = document.querySelectorAll('li')
const randomElements = [...els]
  .sort(() => {
    return Math.random() - 0.5
  })
  .slice(0, 5)
console.log(randomElements)

to select all the li’s with querySelectorAll.

Then we spread the els into an array.

Next, we call sort on the elements array with a callback that picks a random number that can be positive or negative to shuffle the array.

Then we call slice with 0 and 5 to return the first 5 elements from the shuffled array.

Conclusion

To select 5 random elements with JavaScript, we can spread the elements into an array.

Then we can use the array sort method to shuffle the array.

And we use slice to pick the first 5 items.