views:

45

answers:

5

I am trying to see how to find a tag that was added dynamically within a table as so:

ParentTag= document.getElementById('parentdiv');
var newTag = document.createElement('div');
newTag.innerHTML="<span class="ImNew"></span>" 
ParentTag.appendChild(newTag);

How will I be able to find that tag in javascript, not leaning towards using jquery so no live recommendations please.. Trying to find that new tag in strictly javascript.

The tag I am referring to is the span tag

+2  A: 

You could give your new tag an ID when you create it:

ParentTag= document.getElementById('parentdiv');
var newTag = document.createElement('div');
newTag.setAttribute('id','myNewID');
newTag.innerHTML="<span class="ImNew"></span>" 
ParentTag.appendChild(newTag);

Then later, just use that ID to find it:

var newTag = document.getElementById('myNewID');
Justin Ethier
sorry, if I mislead you, but I would be trying to find that span tag
Jake
OK, was not clear on that. Can you still use the same technique of finding it by ID, or is that no good for your needs?
Justin Ethier
no, the id is no good in this situation
Jake
A: 

Well, live wouldn't get you anywhere anyway, because it only works for events. Why doesn't something like document.getElementsByClassName('ImNew') not work?

Matt Ball
Well, partly because there is no such method... ;)
Guffa
Sorry, I missed an 's'. https://developer.mozilla.org/en/DOM/document.getElementsByClassName - I stick with jQuery selectors for the most part because the equivalent native DOM code is hellishly verbose.
Matt Ball
A: 

Not sure if this is relevant, but isn't it the wrong way around to try to find it whilst the nice thing with dynamically created DOM objects is that you can actually access them via a previously stored reference (this case 'ParentTag') ?

Theo.T
+1  A: 

As others have mentioned, why not just keep track of it or give it an id if you're going to need it later?

But anyway, this is how you could do a manual search (searching from back to front as you're adding the new items to the end).

var parent = document.getElementById("parentdiv")
for (var i = parent.childNodes.length - 1; i >= 0; i--)
{
    var el = parent.childNodes[i];
    if (el.nodeType == 1 &&
        el.nodeName.toLowerCase() == "div" &&
        el.firstChild &&
        el.firstChild.nodeName.toLowerCase() == "span" &&
        el.firstChild.className = "ImNew")
    {
        // el.firstChild is what you want
    }
}
insin
+1  A: 

That depends on what other elements exist in the element. You can for example get all span tags in the element and filter out the ones with a specific class name:

var parentTag = document.getElementById('parentdiv');
var spans = parentTag.getElementsByTagname('SPAN');
var filtered = [];
for (var i=0; i<spans.length; i++) {
  if (spans[i].className === 'ImNew') filtered.push(spans[i]);
}

If there is only one span tag with that class name in the element, the filtered array now cotnains a reference to that element.

Guffa
well when I look at the view source, that added span element is not there, but the content within it is displayed on the page, so are you sure I would be able to find it by just traversing the span tags
Jake
@Jake that's your browser showing you the page source rather than the current contents of the DOM. If you inserted it, it will be there.
insin
You can use Firebug to see the actual contents of the DOM in real time, which would be a big help.
Justin Ethier
thanks, you were right, the source fooled me
Jake