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);

“getElementById not a function” when trying to parse an AJAX response?

Use DOMParser() to convert responseText into a searchable DOM tree. Also, your attempts to search/use anything derived from responseText, must occur inside the onload function. Use code like this: GM_xmlhttpRequest ( { … onload: parseAJAX_ResponseHTML, … } ); function parseAJAX_ResponseHTML (respObject) { var parser = new DOMParser (); var responseDoc = parser.parseFromString (respObject.responseText, “text/html”); console.log … Read more

Can I use document.getElementById() with multiple ids?

document.getElementById() only supports one name at a time and only returns a single node not an array of nodes. You have several different options: You could implement your own function that takes multiple ids and returns multiple elements. You could use document.querySelectorAll() that allows you to specify multiple ids in a CSS selector string . … Read more

Getting element by a custom attribute using JavaScript

It is not good to use custom attributes in the HTML. If any, you should use HTML5’s data attributes. Nevertheless you can write your own function that traverses the tree, but that will be quite slow compared to getElementById because you cannot make use of any index: function getElementByAttribute(attr, value, root) { root = root … Read more

Get element inside element by class and ID – JavaScript

Well, first you need to select the elements with a function like getElementById. var targetDiv = document.getElementById(“foo”).getElementsByClassName(“bar”)[0]; getElementById only returns one node, but getElementsByClassName returns a node list. Since there is only one element with that class name (as far as I can tell), you can just get the first one (that’s what the [0] … Read more

chaining getElementById

Nope. …But you can, though: Element.prototype.getElementById = function(id) { return document.getElementById(id); } Try it on this page: var x = document.getElementById(‘footer’).getElementById(‘copyright’); Edit: As Pumbaa80 pointed out, you wanted something else. Well, here it is. Use with caution. Element.prototype.getElementById = function(req) { var elem = this, children = elem.childNodes, i, len, id; for (i = 0, … Read more

Mulitple Elements with the same ID

Id must be unique. You should use class instead and then make use of document.getElementsByClassName(‘className’); var video = document.getElementsByClassName(‘vid’); var myFunction = function() { // Reset the video to this.currentTime = 0; // And play again this.load(); }; for (var i = 0; i < video.length; i++) { video[i].addEventListener(‘ended’, myFunction, false); }

Getting the parent div of element

You’re looking for parentNode, which Element inherits from Node: parentDiv = pDoc.parentNode; Handy References: DOM2 Core specification – well-supported by all major browsers DOM2 HTML specification – bindings between the DOM and HTML DOM3 Core specification – some updates, not all supported by all major browsers HTML5 specification – which now has the DOM/HTML bindings … Read more

tech