Categories
JavaScript Answers

How to show a hidden div when a select option is selected with JavaScript?

Sometimes, we want to show a hidden div when a select option is selected with JavaScript.

In this article, we’ll look at how to show a hidden div when a select option is selected with JavaScript.

How to show a hidden div when a select option is selected with JavaScript?

To show a hidden div when a select option is selected with JavaScript, we can listen to the change event of the select element to get the option selected.

Then we can set the display CSS property of the element we want to show or hide according to the selected value.

For instance, we write:

<select id="select">
  <option value="0">No</option>
  <option value="1">Yes</option>
</select>

<div id="div" style="display: none;">
  Hello hidden content
</div>

to add a select element and a div to show or hide according to the selected drop down value.

Then we write:

const select = document.getElementById('select')
const div = document.querySelector('div')

select.addEventListener('change', (e) => {
  if (+e.target.value === 1) {
    div.style.display = 'block'
  } else {
    div.style.display = 'none'
  }
})

to show or hide the div when we select a new value in the drop down.

Then we use document.getElementById to select each element.

Next, we call select.addEventListener with 'change' and the change event listener to add a change event listener.

In the event listener, we check if e.target.value is 1 after it’s converted to a number.

If that’s true, we set div.style.display to 'block' to display it.

Otherwise, we set display.style.display to hide it.

Conclusion

To show a hidden div when a select option is selected with JavaScript, we can listen to the change event of the select element to get the option selected.

Then we can set the display CSS property of the element we want to show or hide according to the selected value.

Categories
JavaScript Answers

How to add a list item with JavaScript?

Sometimes, we want to add a list item with JavaScript.

In this article, we’ll look at how to add a list item with JavaScript.

How to add a list item with JavaScript?

To add a list item with JavaScript, we can call the document.createElement method to create an li element.

Then we call appenddChild on the list with the li element object as the argument.

For instance, we write:

<ol id="demo"></ol>

to add the ol element.

Then we write:

const list = document.getElementById('demo');
const firstName = 'jane'
const entry = document.createElement('li');
entry.appendChild(document.createTextNode(firstName));
list.appendChild(entry);

to select the ol element with document.getElementById.

Then we create the li element with document.createElement.

Next, we call document.createTextNode with firstName to create a text node with the text.

Then we call entry.appendChild to add the text node to the li.

Finally, we call list.appendChild with entry to add the li to the ol.

Now we should see:

1. jane

displayed.

Conclusion

To add a list item with JavaScript, we can call the document.createElement method to create an li element.

Then we call appenddChild on the list with the li element object as the argument.

Categories
JavaScript Answers

How to get the filename from a string path in JavaScript?

Sometimes, we want to get the filename from a string path in JavaScript.

In this article, we’ll look at how to get the filename from a string path in JavaScript.

How to get the filename from a string path in JavaScript?

To get the filename from a string path in JavaScript, we can use the JavaScript string’s split method and the JavaScript array’s pop method.

For instance, we write:

const nameString = "/app/base/controllers/filename.js";
const fileName = nameString.split("/").pop();
console.log(fileName)

to call split on nameString with '/' to split the nameString path into segments.

Then we call pop to return the last entry from the split string.

Therefore, we should see that fileName is 'filename.js' according to the console log.

Conclusion

To get the filename from a string path in JavaScript, we can use the JavaScript string’s split method and the JavaScript array’s pop method.

Categories
JavaScript Answers

How to check if a string is a palindrome with JavaScript?

Sometimes, we want to check if a string is a palindrome with JavaScript.

In this article, we’ll look at how to check if a string is a palindrome with JavaScript.

How to check if a string is a palindrome with JavaScript?

To check if a string is a palindrome with JavaScript, we can use the JavaScript string’s split and JavaScript array’s reverse and join methods.

For instance, we write:

const isPalindrome = (s) => {
  return s === s.split("").reverse().join("");
}

console.log(isPalindrome('foobar'))
console.log(isPalindrome('abba'))

to create the isPalindrom function to check the string s is the same as the original after we reversed the letters in it.

We reverse s by splitting it with split with an empty string to return an array of characters in the string.

Then we call reverse to reverse the array.

And we call join with an empty string to join the characters back into a string.

Finally, we return s compared with the reversed version of s with ===.

Therefore, from the console log, we should see false and then true logged respectively.

Conclusion

To check if a string is a palindrome with JavaScript, we can use the JavaScript string’s split and JavaScript array’s reverse and join methods.

Categories
JavaScript Answers

How to Convert a Map to JSON Object with JavaScript?

Sometimes, we want to convert a map to JSON object with JavaScript.

In this article, we’ll look at how to convert a map to JSON object with JavaScript.

How to Convert a Map to JSON Object with JavaScript?

To convert a map to JSON object with JavaScript, we can use the Object.fromEntries method.

For instance, we write:

const map1 = new Map([
  ['foo', 'bar'],
  ['baz', 12]
]);

const obj = Object.fromEntries(map1);
console.log(obj)

then we get that obj is {foo: 'bar', baz: 12} according to the console log.

Conclusion

To convert a map to JSON object with JavaScript, we can use the Object.fromEntries method.