Tag: dom

How to tell if an element is a child of another element in JavaScript (no jQuery)?

If I have 2 HTML elements, how can I easily tell if 1 is a child of the other? var parent = document.getElementById(‘shapes’); var child = document.getElementById(‘star’); Is there any kind of function on an element like child.getParentElement() that I could compare with like child.getParentElement() == parent? I understand parent.children gives me back an array […]

Finding the location of a TD in a table

So let’s say I have a table, and I want to manipulate a specific <td> in it: HTML: <table> <tr><td>1</td> <td>2</td></tr> <tr><td>3</td> <td>4</td></tr> <tr><td id=”hi”>5</td> <td>6</td></tr> </table> Javascript: document.getElementsByTagName(“table”)[0].rows[2].cells[0]; This will help me REACH a specific cell in a table. My question is this: Say I have a specific <td> inside a table: var td […]

Does jQuery can use an end tag as a selector?

I am pretty new in jQuery. I cam across this jQuery code that uses <div /> as a selector: $(“<div />”).html(someString).text(); First of all, I would like to know if it is a legitimate tag to use, since div has an end tag: </div>. But my main question – does jQuery can use an end […]

How to get the value of data attribute using jquery

I am working on a node.js application which generates a html page. This html page displays a list of associates built according to the data passed onto this page. A list is built something like as follows: <ul class=”notification-body” style=””> //loop for all assocaite id’s passed to this page <li class=”testClass” data-associateid=”<%= assocID %>”> <span> […]

Detecting user tampering with DOM

I’ve written a web app that uses an ad banner on the side to support free accounts. Problem is, since the ad is loaded in its own <div>, it’s relatively easy to go into developer/debug console in any modern browser and either remove the div or resize the ad to 0px using CSS overrides (the […]

“push” is not a function [on elements returned by getElementsByTagName]

I have a bit of Javascript where I’m POSTing my form with an XMLHttpRequest, which otherwise works fine, but when I try to add the SELECT values to the array of INPUT values, it seems Javascript has lost its mind (or I have). This function just builds the “name1=value1&name2=value2” string and passes it along. The […]

How do I remove an element from the DOM, given its id?

In this specific case, the element is a table row.

How to keep new <li> Elements on the top using Javascript

I am trying to add new <li> elements to the top (instead of at the bottom). I have come up with basic code which makes use of document.appendChild(). Since there is no prependChild() I couldn’t figure out a way to get it done. Here is the demo. http://jsfiddle.net/pwpSx/ Once you run it. All <li> elements […]

Efficient milliseconds display in HTML

The goal: find the most CPU efficient way to display the current milliseconds in HTML. My current solution takes up to 10% CPU computation on my laptop. My current solution: http://dev.timerintab.appspot.com/test/ms My final goal is to display the milliseconds on a timer. UpdateI changed the test to take an argument for the interval: http://dev.timerintab.appspot.com/test/ms?interval=x where […]

Why can't the same node be inserted at two different places in a document?

I am trying to insert a button node in a document but for some reasons, it’s not getting inserted at both places. the code is below: var elements = document.querySelectorAll(‘.Tabelle-Titel-nur-oben’); var buttonElement = document.createElement(“Button”); var t0 = document.createTextNode(“CLICK ME”); buttonElement.appendChild(t0); for(var i = 0; i< elements.length; i++) { document.body.insertBefore(buttonElement, elements[i]); } In my code, there […]