I’m looking for ideas on how to detect objects that are in the HTML DOM, but they are not represented explicitly in the HTML source.
As you may know, if I have HTML source that contains the following:
<table id="myTbl">
<tr id="row1">
<td id="name">George</td>
</tr>
</table>
…the HTML DOM will add the <tbody> object in the object tree without changing the source code, understanding that the source code implies it. So in the DOM, the structure is as if the HTML source had been:
<table id="myTbl">
<tbody>
<tr id="row1">
<td id="name">George</td>
</tr>
</tbody>
</table>
I have a javascript function that is going through the DOM tree, and I need to detect when I have run across an object that is implied, that is, it is in the DOM, but not in the HTML source.
Any ideas how to do this? Maybe there is some attribute in the object that tells whether it originated from the source or not?
New idea based on Alex’s idea. Still involves re-fetching the entire DOM, but it’s impossible to do otherwise. You could implement this same logic server-side if it’s feasible in your app. Using jQuery for brevity:
that will give you a list of all elements inserted 🙂 May need some refinement , e.g. around the quotes matching
class="since a quote is technically optional there. Also un-tested, but should work.Note that you’re getting new elements back with this method, not the ones that currently exist in your DOM, so if that matters just be aware of it.