How may I reference the script tag that loaded the currently-executing script?

How to get the current script element: 1. Use document.currentScript document.currentScript will return the <script> element whose script is currently being processed. <script> var me = document.currentScript; </script> Benefits Simple and explicit. Reliable. Don’t need to modify the script tag Works with asynchronous scripts (defer & async) Works with scripts inserted dynamically Problems Will not … Read more

How can I loop through enum values for display in radio buttons? [duplicate]

Two options: for (let item in MotifIntervention) { if (isNaN(Number(item))) { console.log(item); } } Or Object.keys(MotifIntervention).filter(key => !isNaN(Number(MotifIntervention[key]))); (code in playground) Edit String enums look different than regular ones, for example: enum MyEnum { A = “a”, B = “b”, C = “c” } Compiles into: var MyEnum; (function (MyEnum) { MyEnum[“A”] = “a”; MyEnum[“B”] … Read more

How to get the focused element with jQuery?

// Get the focused element: var $focused = $(‘:focus’); // No jQuery: var focused = document.activeElement; // Does the element have focus: var hasFocus = $(‘foo’).is(‘:focus’); // No jQuery: elem === elem.ownerDocument.activeElement; Which one should you use? quoting the jQuery docs: As with other pseudo-class selectors (those that begin with a “:”), it is recommended … Read more

jQuery counting elements by class – what is the best way to implement this?

Should just be something like: // Gets the number of elements with class yourClass var numItems = $(‘.yourclass’).length As a side-note, it is often beneficial to check the length property before chaining a lot of functions calls on a jQuery object, to ensure that we actually have some work to perform. See below: var $items … Read more

How can I check if an element exists in the visible DOM?

It seems some people are landing here, and simply want to know if an element exists (a little bit different to the original question). That’s as simple as using any of the browser’s selecting method, and checking it for a truthy value (generally). For example, if my element had an id of “find-me”, I could … Read more

Deleting array elements in JavaScript – delete vs splice

delete will delete the object property, but will not reindex the array or update its length. This makes it appears as if it is undefined: > myArray = [‘a’, ‘b’, ‘c’, ‘d’] [“a”, “b”, “c”, “d”] > delete myArray[0] true > myArray[0] undefined Note that it is not in fact set to the value undefined, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)