Categories
JavaScript Answers

How to only trigger parent click event when a child is clicked with JavaScript?

Sometimes, we want to only trigger parent click event when a child is clicked with JavaScript.

In this article, we’ll look at how to only trigger parent click event when a child is clicked with JavaScript.

How to only trigger parent click event when a child is clicked with JavaScript?

To only trigger parent click event when a child is clicked with JavaScript, we call stopPropagation.

For instance, we write

parent.addEventListener(
  "click",
  (e) => {
    e.stopPropagation();
    console.log("event on parent!");
  },
  true
);

to call addEventListener to listen for clicks on the parent element.

In the event handler, we call stopPropagation to stop the event from going down to the child.

We call it with true to make events propagate down the DOM hierarchy instead of up.

Conclusion

To only trigger parent click event when a child is clicked with JavaScript, we call stopPropagation.

Categories
JavaScript Answers

How to set max viewport in Puppeteer with JavaScript?

Sometimes, we want to set max viewport in Puppeteer with JavaScript.

In this article, we’ll look at how to set max viewport in Puppeteer with JavaScript.

How to set max viewport in Puppeteer with JavaScript?

To set max viewport in Puppeteer with JavaScript, we call puppeeter.launch with an object that sets the defaultViewport option.

For instance, we write

const browser = await puppeteer.launch({ defaultViewport: null });

to set defaultViewport to null to make Puppeteer open a window bigger than 800px x 600px.

Conclusion

To set max viewport in Puppeteer with JavaScript, we call puppeeter.launch with an object that sets the defaultViewport option.

Categories
JavaScript Answers

How to test if a variable does not equal either of two values with JavaScript?

Sometimes, we want to test if a variable does not equal either of two values with JavaScript.

In this article, we’ll look at how to test if a variable does not equal either of two values with JavaScript.

How to test if a variable does not equal either of two values with JavaScript?

To test if a variable does not equal either of two values with JavaScript, we use boolean operators.

For instance, we write

if (test !== "A" && test !== "B") {
  //...
}

to check if test isn’t 'A' and also isn’t 'B'.

Conclusion

To test if a variable does not equal either of two values with JavaScript, we use boolean operators.

Categories
JavaScript Answers

How to use JavaScript execCommand to copy hidden text to clipboard?

Sometimes, we want to use JavaScript execCommand to copy hidden text to clipboard.

In this article, we’ll look at how to use JavaScript execCommand to copy hidden text to clipboard.

How to use JavaScript execCommand to copy hidden text to clipboard?

To use JavaScript execCommand to copy hidden text to clipboard, we convert it to a text input first.

For instance, we write

<input id="dummy" name="dummy" type="hidden" />

to add a hidden input.

Then we write

const copyText = document.getElementById("dummy");
copyText.type = "text";
copyText.select();
document.execCommand("copy");
copyText.type = "hidden";

to get the hidden input with getElementById.

We then set the type of the input to 'text'.

Next we call select to select the text inside.

Then we call execCommand with 'copy' to copy the selected text into the clipboard.

And finally we set its type back to 'hidden'.

Conclusion

To use JavaScript execCommand to copy hidden text to clipboard, we convert it to a text input first.

Categories
JavaScript Answers

How to convert 24-hour time-of-day string to 12-hour time with AM/PM and no time zone with JavaScript?

Sometimes, we want to convert 24-hour time-of-day string to 12-hour time with AM/PM and no time zone with JavaScript.

In this article, we’ll look at how to convert 24-hour time-of-day string to 12-hour time with AM/PM and no time zone with JavaScript.

How to convert 24-hour time-of-day string to 12-hour time with AM/PM and no time zone with JavaScript?

To convert 24-hour time-of-day string to 12-hour time with AM/PM and no time zone with JavaScript, we use the toLoclaeTimeString method.

For instance, we write

const timeString = "18:00:00";

const timeString12hr = new Date(
  "1970-01-01T" + timeString + "Z"
).toLocaleTimeString("en-US", {
  timeZone: "UTC",
  hour12: true,
  hour: "numeric",
  minute: "numeric",
});

to create a date from timeString.

Then we call toLocaleTimeString with the locale and an object with the options.

We set hour12 to true to make it return a 12 hour time string.

Conclusion

To convert 24-hour time-of-day string to 12-hour time with AM/PM and no time zone with JavaScript, we use the toLoclaeTimeString method.