Categories
JavaScript Answers

How to hide an HTML element by ID with JavaScript?

Sometimes, we want to hide an HTML element by ID with JavaScript.

In this article, we’ll look at how to hide an HTML element by ID with JavaScript.

How to hide an HTML element by ID with JavaScript?

To hide an HTML element by ID with JavaScript, we can set the style.display property to 'none' or the style.visibility property to 'hidden'.

For instance, we write:

<div class="nav">
  <ul>
    <li>
      <a id="nav-ask" href="/questions/ask">
        Ask Question
      </a>
    </li>
  </ul>
</div>

to add some elements.

Then we can hide the a element by writing:

const link = document.getElementById('nav-ask');
link.style.display = 'none';

or

const link = document.getElementById('nav-ask');
link.style.visibility = 'hidden';

Conclusion

To hide an HTML element by ID with JavaScript, we can set the style.display property to 'none' or the style.visibility property to 'hidden'.

Categories
JavaScript Answers

How to store compressed JSON data in local storage with JavaScript?

Sometimes, we want to store compressed JSON data in local storage with JavaScript.

In this article, we’ll look at how to store compressed JSON data in local storage with JavaScript.

How to store compressed JSON data in local storage with JavaScript?

To store compressed JSON data in local storage with JavaScript, we can use the lz-string library.

For instance, we write:

<script src="https://cdnjs.cloudflare.com/ajax/libs/lz-string/1.4.4/lz-string.min.js"></script>

to add the library script.

Then we write:

const jsonobj = {
  'sample': 'This is supposed to be ling string',
  'score': 'another long string which is going to be compressed'
}

localStorage.setItem('mystring', LZString.compress(JSON.stringify(jsonobj)));

const string = LZString.decompress(localStorage.getItem('mystring'))
console.log(JSON.parse(string));

to call LZString.compress to compress the stringified version of jsonobj.

Then we decompress the string with LZString.decompress.

And then we call JSON.parse to parse the decompressed string.

Conclusion

To store compressed JSON data in local storage with JavaScript, we can use the lz-string library.

Categories
JavaScript Answers

How to programmatically use CSS transitions with JavaScript?

Sometimes, we want to programmatically use CSS transitions with JavaScript.

In this article, we’ll look at how to programmatically use CSS transitions with JavaScript.

How to programmatically use CSS transitions with JavaScript?

To programmatically use CSS transitions with JavaScript, we can use an element’s animate method.

For instance, we write:

<img src='https://www.macmillandictionary.com/external/slideshow/full/emoji_snowflake_full.jpg' style='width: 100px'>

to add an image to animate.

Then we write:

const snowFlake = document.querySelector('img')
const snowLeft = 200
const player = snowFlake.animate(
  [{
      transform: `translate(${snowLeft}px, -100%)'}`
    },
    {
      transform: `translate(${snowLeft}px, ${window.innerHeight}px)`
    }
  ],
  1500);

setTimeOut(() => {
  player.cancel();
}, 2000)

We select the image with document.querySelector.

Then we call snowFlake.animate with an array of key frames.

We set the transform property to move the image.

And we specify the duration to be 1500ms.

Finally, we call player.cancel to stop the animation after 2000ms.

Conclusion

To programmatically use CSS transitions with JavaScript, we can use an element’s animate method.

Categories
JavaScript Answers

How to display PDF from an array buffer with JavaScript?

Sometimes, we want to display PDF from an array buffer with JavaScript.

In this article, we’ll look at how to display PDF from an array buffer with JavaScript.

How to display PDF from an array buffer with JavaScript?

To display PDF from an array buffer with JavaScript, we can use the Fetch API.

For instance, we write:

<iframe id='pdfViewer'>

</iframe>

to add an iframe.

Then we write:

const pdfSrc = 'https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/examples/learning/helloworld.pdf';

(async () => {
  const res = await fetch(pdfSrc)
  const data = await res.arrayBuffer()
  $("#pdfViewer").attr("src", URL.createObjectURL(new Blob([data], {
    type: "application/pdf"
  })))
})()

to call fetch with the pdfSrc to fetch the PDF binary.

Then we call res.arrayBuffer to assign the response to data.

Next, we call attr to set the src attribute to a URL that we create from the data PDF binary by converting that to a Blob instance.

If CORS is allowed, then the PDF should be displayed in the iframe.

Conclusion

To display PDF from an array buffer with JavaScript, we can use the Fetch API.

Categories
JavaScript Answers

How to add an embeddable WYSIWYG equation editor with JavaScript?

Sometimes, we want to add an embeddable WYSIWYG equation editor with JavaScript.

In this article, we’ll look at how to add an embeddable WYSIWYG equation editor with JavaScript.

How to add an embeddable WYSIWYG equation editor with JavaScript?

To add an embeddable WYSIWYG equation editor with JavaScript, we can use the MathQuill library.

For instance, we write:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/mathquill/0.10.1/mathquill.min.css">`

<script src="https://cdnjs.cloudflare.com/ajax/libs/mathquill/0.10.1/mathquill.min.js" type="text/javascript"></script>

<div>

</div>

to add the scripts and styles required for MathQuill.

We also add a div that’s used as the container for the editor.

Then we write:

const htmlElement = document.querySelector('div');
const config = {
  handlers: {
    edit() {}
  },
  restrictMismatchedBrackets: true
};
const MQ = MathQuill.getInterface(2);
const mathField = MQ.MathField(htmlElement, config);
mathField.latex('2^{\\frac{3}{2}}');

to select the div with document.querySelector.

And then we call MathQuill.getInterface to return a MathQuill instance.

And then we call MQ.MathField to add an equation input.

Next, we call latex with some Latex code to add some math expressions.

Conclusion

To add an embeddable WYSIWYG equation editor with JavaScript, we can use the MathQuill library.