How to add a class to a given element?

If you’re only targeting modern browsers: Use element.classList.add to add a class: And element.classList.remove to remove a class: If you need to support Internet Explorer 9 or lower: Add a space plus the name of your new class to the className property of the element. First, put an id on the element so you can easily get a reference. Then Note the space … Read more

“SyntaxError: Unexpected token < in JSON at position 0"

The wording of the error message corresponds to what you get from Google Chrome when you run JSON.parse(‘<…’). I know you said the server is setting Content-Type:application/json, but I am led to believe the response body is actually HTML. Feed.js:94 undefined “parsererror” “SyntaxError: Unexpected token < in JSON at position 0” with the line console.error(this.props.url, status, err.toString()) underlined. The err was actually thrown … Read more

Define a global variable in a JavaScript function

As the others have said, you can use var at global scope (outside of all functions and modules) to declare a global variable: (Note that that’s only true at global scope. If that code were in a module — <script type=”module”>…</script> — it wouldn’t be at global scope, so that wouldn’t create a global.) Alternatively: In modern environments, you can assign to … Read more

Google Chrome Uncaught (in promise) DOMException while playing AUDIO

You’re receiving an uncaught exception because you aren’t handling an error. Audio is an HTMLMediaElement object, and the play() method returns a promise. Therefore I recommend handling the error. One of two errors are possible: NotSupportedError This means that the audio source is not supported by the browser (probably due to audio format) NotAllowedError This is the one I … Read more

How do I redirect to another webpage?

One does not simply redirect using jQuery jQuery is not necessary, and window.location.replace(…) will best simulate an HTTP redirect. window.location.replace(…) is better than using window.location.href, because replace() does not keep the originating page in the session history, meaning the user won’t get stuck in a never-ending back-button fiasco. If you want to simulate someone clicking on a link, use location.href If you … Read more

What does this symbol () => mean in Javascript? [duplicate]

It’s an arrow function, newly defined in ES6. An arrow function expression has a shorter syntax than a function expression and does not bind its own this, arguments, super, or new.target. Arrow functions are always anonymous. These function expressions are best suited for non-method functions, and they cannot be used as constructors. They are often just a shorter way of writing the anonymous … Read more

How to format a JavaScript date

For custom-delimited date formats, you have to pull out the date (or time) components from a DateTimeFormat object (which is part of the ECMAScript Internationalization API), and then manually create a string with the delimiters you want. To do this, you can use DateTimeFormat#formatToParts. You could destructure the array, but that is not ideal, as … Read more