Categories
JavaScript Answers Vue Vue Answers

How to check if image URL is valid or broken with Vue.js?

Sometimes, we want to check if image URL is valid or broken with Vue.js.

In this article, we’ll look at how to check if image URL is valid or broken with Vue.js.

How to check if image URL is valid or broken with Vue.js?

To check if image URL is valid or broken with Vue.js, we can handle the error event triggered by the img 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: `
    <div>
    	<img src='abc' @error='onError'>
    </div>
  `,
  methods: {
    onError() {
      console.log('img failed to load')
    }
  }
})

to add an img element into the template.

And we set @error to onError to run onError when the image fails to load.

Since the img element’s src attribute isn’t a valid URL, onError will run.

Conclusion

To check if image URL is valid or broken with Vue.js, we can handle the error event triggered by the img element.

Categories
JavaScript Answers

How to check if all elements in array are strings with JavaScript?

Sometimes, we want to check if all elements in array are strings with JavaScript.

In this article, we’ll look at how to check if all elements in array are strings with JavaScript.

How to check if all elements in array are strings with JavaScript?

To check if all elements in array are strings with JavaScript, we can use the array every method and the typeof operator.

For instance, we write:

const check = arr => arr.every(i => (typeof i === "string"));

console.log(check(['foo', 'bar']))
console.log(check(['foo', 'bar', 1]))

to define the check function that calls arr.every with a callback that checks of each entry i is a string with typeof.

As a result, the first console log logs true and the 2nd one logs false since the first array have all string entries but the 2nd array has one entry that’s not a string.

Conclusion

To check if all elements in array are strings with JavaScript, we can use the array every method and the typeof operator.

Categories
JavaScript Answers

How to map multiple arrays with JavaScript?

Sometimes, we want to map multiple arrays with JavaScript.

In this article, we’ll look at how to map multiple arrays with JavaScript.

How to map multiple arrays with JavaScript?

To map multiple arrays with JavaScript, we can use the map method.

For instance, we write:

const zip = (a1, a2) => a1.map((x, i) => [x, a2[i]]);

const arr1 = ['a', 'b', 'c'];
const arr2 = [1, 2, 3];
console.log(zip(arr1, arr2))

to define the zip function that calls a1.map to combine the entries from a1 and a2 with index i into one entry.

Then we call zip with arr1 and arr2 to zip them into one array.

As a result, the array logged is [["a", 1], ["b", 2], ["c", 3]].

Conclusion

To map multiple arrays with JavaScript, we can use the map method.

Categories
JavaScript Answers

How to convert a camel-case string to dashes in JavaScript?

Sometimes, we want to convert a camel-case string to dashes in JavaScript.

In this article, we’ll look at how to convert a camel-case string to dashes in JavaScript.

How to convert a camel-case string to dashes in JavaScript?

To convert a camel-case string to dashes in JavaScript, we can use the string’s replace method.

For instance, we write:

const camel = 'fooBar'
const dashed = camel.replace(/[A-Z]/g, m => "-" + m.toLowerCase());
console.log(dashed)

to call camel.replace with a regex to match all capital letters.

And then we pass in a callback that replace the matches with a dash and the matched letter converted to lower case concatenated to it.

As a result, dashed is "foo-bar".

Conclusion

To convert a camel-case string to dashes in JavaScript, we can use the string’s replace method.

Categories
JavaScript Answers

How to count and limit words in a text area with JavaScript?

Sometimes, we want to count and limit words in a text area with JavaScript.

In this article, we’ll look at how to count and limit words in a text area with JavaScript.

How to count and limit words in a text area with JavaScript?

To count and limit words in a text area with JavaScript, we can check the length of the text area’s value in the keypress event handler.

For instance, we write:

<textarea></textarea>

to add a text area.

Then we write:

const textarea = document.querySelector('textarea')

textarea.onkeypress = (e) => {
  if (e.target.value.length > 20) {
    e.preventDefault()
  }
};

to select the text area with querySelector.

Next, we set textarea.onkeypress to a function that checks if the text area’s input value is bigger than 20.

If it is, then we call e.preventDefault to stop appending the typed characters to the text area’s input value.

Conclusion

To count and limit words in a text area with JavaScript, we can check the length of the text area’s value in the keypress event handler.