Categories
JavaScript Answers

How to remove all option of dropdown box in JavaScript?

Sometimes, we want to remove all option of dropdown box in JavaScript.

In this article, we’ll look at how to remove all option of dropdown box in JavaScript.

How to remove all option of dropdown box in JavaScript?

To remove all option of dropdown box in JavaScript, we can set the options.length property of the select element to 0.

For instance, we write:

<select>
  <option>foo</option>
  <option>bar</option>
</select>

to add the select drop down.

Then we write:

document.querySelector('select').options.length = 0;

to remove all the option elements from the drop down.

We select the select element with document.querySelector('select').

And then we set the options.length property of it to 0 to remove all the options.

Conclusion

To remove all option of dropdown box in JavaScript, we can set the options.length property of the select element to 0.

Categories
JavaScript Answers

How to update existing URL query string values with JavaScript?

Sometimes, we want to update existing URL query string values with JavaScript.

In this article, we’ll look at how to update existing URL query string values with JavaScript.

How to update existing URL query string values with JavaScript?

To update existing URL query string values with JavaScript, we can use the URL constructor.

For instance, we write:

const currentUrl = 'http://www.example.com/hello.png?w=100&h=100&bg=white';
const url = new URL(currentUrl);
url.searchParams.set("w", "200"); 
const newUrl = url.href; 
console.log(newUrl);

to create a new URL instance from currentUrl.

Then we call url.searchParams.set to update the w query parameter to have value 200.

Then we get the new URL with url.href.

Therefore, newUrl is 'http://www.example.com/hello.png?w=200&h=100&bg=white'.

Conclusion

To update existing URL query string values with JavaScript, we can use the URL constructor.

Categories
JavaScript Answers

How to compare two JavaScript regexes?

Sometimes, we want to compare two JavaScript regexes.

In this article, we’ll look at how to compare two JavaScript regexes.

How to compare two JavaScript regexes?

To compare two JavaScript regexes, we can convert JavaScript regex objects to strings before comparing.

For instance, we write:

const regexp1 = /a/;
const regexp2 = /a/;
const isSame = String(regexp1) === String(regexp2)
console.log(isSame)

to convert regexp1 and regexp2 to strings with String before doing the comparison.

Therefore, isSame is true since they have the same content.

Conclusion

To compare two JavaScript regexes, we can convert JavaScript regex objects to strings before comparing.

Categories
JavaScript Answers

How to convert a string to an object with JavaScript?

Sometimes, we want to convert a string to an object with JavaScript.

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

How to convert a string to an object with JavaScript?

To convert a string to an object with JavaScript, we can use the JSON.parse method.

For instance, we write:

const str = '{"a":"www"}';
const obj = JSON.parse(str);
console.log(obj)

to call JSON.parse with str to return an object from the str JSON string.

Therefore, obj is {a: 'www'}.

Conclusion

To convert a string to an object with JavaScript, we can use the JSON.parse method.

Categories
JavaScript Answers Vue Vue Answers

How to get the content of a contentEditable element with Vue.

Sometimes, we want to get the content of a contentEditable element with Vue.js.

In this article, we’ll look at how to get the content of a contentEditable element with Vue.js.

How to get the content of a contentEditable element with Vue.js?

To get the content of a contentEditable element with Vue.js, we can listen to the input event on the contentEditable element.

For instance, we write:

<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

<div id='app'>

</div>

to add the Vue script and app container.

Then we write:

const v = new Vue({
  el: '#app',
  template: `
    <p
      contenteditable
      @input="onInput"
    >
      {{ content }}
    </p>
  `,
  data: {
    content: 'hello world'
  },
  methods: {
    onInput(e) {
      console.log(e.target.innerText);
    },
  },
})

We have a contentEditable p element.

And we listen to the input event with @input="onInput".

And we get the content of the div when it’s being changed with e.target.innerText.

We put the initial content of the p element in content.

Conclusion

To get the content of a contentEditable element with Vue.js, we can listen to the input event on the contentEditable element.