getElementById returns null? [closed]

Also be careful how you execute the js on the page. For example if you do something like this: (function(window, document, undefined) { var foo = document.getElementById(“foo”); console.log(foo); })(window, document, undefined); This will return null because you’d be calling the document before it was loaded. Use window.onload to wait for the dom nodes to load: … Read more

vue.js ‘document.getElementById’ shorthand

Theres no shorthand way in vue 2. Jeff’s method seems already deprecated in vue 2. Heres another way u can achieve your goal. var app = new Vue({ el:’#app’, methods: { showMyDiv() { console.log(this.$refs.myDiv); } } }); <script src=”https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js”></script> <div id=’app’> <div id=”myDiv” ref=”myDiv”></div> <button v-on:click=”showMyDiv” >Show My Div</button> </div>

Why don’t we just use element IDs as identifiers in JavaScript?

Anyway, this method seems to be quite poorly documented, and In fact, the sources I come across don’t even give it a mention […] Reliance on implicitly-declared global variables aside, the lack of documentation is a great reason not to use it. The apparent promotion of id values into global variables isn’t standards compliant (the … Read more

Javascript Append Child AFTER Element

You can use: if (parentGuest.nextSibling) { parentGuest.parentNode.insertBefore(childGuest, parentGuest.nextSibling); } else { parentGuest.parentNode.appendChild(childGuest); } But as Pavel pointed out, the referenceElement can be null/undefined, and if so, insertBefore behaves just like appendChild. So the following is equivalent to the above: parentGuest.parentNode.insertBefore(childGuest, parentGuest.nextSibling);