Categories
JavaScript Answers

How to include markdown (.md) files inside HTML files?

Sometimes, we want to include markdown (.md) files inside HTML files.

In this article, we’ll look at how to include markdown (.md) files inside HTML files.

How to include markdown (.md) files inside HTML files?

To include markdown (.md) files inside HTML files, we can use the zero-md web component.

For instance, we write:

<script type="module" src="https://cdn.jsdelivr.net/gh/zerodevx/zero-md@1/src/zero-md.min.js"></script>

<zero-md>
  <script type="text/markdown">
    # **This** is my [markdown](https://example.com)
  </script>
</zero-md>

We add the zero-md web component with a script inside that contains the Markdown code.

It’ll be rendered if we set the type attribute to text/markdown.

Conclusion

To include markdown (.md) files inside HTML files, we can use the zero-md web component.

Categories
JavaScript Answers Vue Vue Answers

How to upload an image in Vue.js?

Sometimes, we want to upload an image in Vue.js.

In this article, we’ll look at how to upload an image in Vue.js.

How to upload an image in Vue.js?

To upload an image in Vue.js, we can add a file input.

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>
      <input type="file" accept="image/*" @change="uploadImage">
      <img :src="previewImage" />
    </p>
  `,
  data: {
    previewImage: undefined
  },
  methods: {
    uploadImage(e) {
      const [image] = e.target.files;
      const reader = new FileReader();
      reader.readAsDataURL(image);
      reader.onload = e => {
        this.previewImage = e.target.result;
        console.log(this.previewImage);
      };
    }
  }
})

We add the file input and img element to preview the image.

Then we set the change event handler of the input to uploadImage.

In uploadImage, we get the first selected file from e.target.files.

Then we create a new FileReader instance and call readAsDataURL with image to read it into a base64 string.

Then we set this.previewImage to `e.target.result, which has the base64 image string.

As a result, when we select an image with the file input, we see the image displayed.

Conclusion

To upload an image in Vue.js, we can add a file input.

Categories
JavaScript Answers

How to add onclick event programmatically with JavaScript?

Sometimes, we want to add onclick event programmatically with JavaScript.

In this article, we’ll look at how to add onclick event programmatically with JavaScript.

How to add onclick event programmatically with JavaScript?

To add onclick event programmatically with JavaScript, we can set the onclick property of an element.

For instance, we write:

<button>
  click me
</button>

to add a button.

Then we write:

const button = document.querySelector('button')
button.onclick = () => {
  console.log('clicked')
}

to select the button with querySelector.

Then we set the button.onclick property to a function that logs 'clicked'.

As a result, we see 'clicked' logged when the button is clicked.

Conclusion

To add onclick event programmatically with JavaScript, we can set the onclick property of an element.

Categories
JavaScript Answers Vue

How to access key from child component with Vue.js?

Sometimes, we want to access key from child component with Vue.js.

In this article, we’ll look at how to access key from child component with Vue.js.

How to access key from child component with Vue.js?

To access key from child component with Vue.js, we should pass in the key value as a prop.

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 the app container.

Then we write:

const Foo = {
  template: `<p>{{index}}</p>`,
  props: ['index']
}

const v = new Vue({
  el: '#app',
  template: `
    <div>
    	<foo :key='i' :index='i' v-for='i of arr' />
    </div>
  `,
  data: {
    arr: [1, 2, 3, 4, 5]
  },
  components: {
    Foo
  }
})

to define the Foo component that takes the index prop.

And then we use v-for to loop through arr and render the foo component with the index prop set to i, which is an entry of arr.

key is reserved so we can’t use it as a prop name.

We also have to register Foo in the components property to render Foo.

Therefore, we see:

1

2

3

4

5

rendered.

Conclusion

To access key from child component with Vue.js, we should pass in the key value as a prop.

Categories
JavaScript Answers

How to find the nth occurrence of a character in a string in JavaScript?

Sometimes, we to find the nth occurrence of a character in a string in JavaScript.

In this article, we’ll look at how to find the nth occurrence of a character in a string in JavaScript.

How to find the nth occurrence of a character in a string in JavaScript?

To find the nth occurrence of a character in a string in JavaScript, we can use the string indexOf method with the from index argument.

For instance, we write:

const indexOfNth = (string, char, nth, fromIndex = 0) => {
  const indexChar = string.indexOf(char, fromIndex);
  if (indexChar === -1) {
    return -1;
  } else if (nth === 1) {
    return indexChar;
  } else {
    return indexOfNth(string, char, nth - 1, indexChar + 1);
  }
}

const str = 'foobarfoobaz'
console.log(indexOfNth(str, 'o', 3))

We define the indexOfNth function that recursively calls string.indexOf with fromIndex as the 2nd argument to start searching from fromIndex.

If indexOf returns -1, we return -1 since char isn’t in string.

If indexOf returns 1, then we found the index of the nth occurrence so we return indexChar.

Otherwise, we keep searching by calling indexOfNth with nth - 1 and indexChar + 1.

Therefore, 7 is logged from the console log since the 3rd o is at index 7.

Conclusion

To find the nth occurrence of a character in a string in JavaScript, we can use the string indexOf method with the from index argument.