Ad

Return Null Document.getElementsByTagName

- 1 answer

ihave simple ul element in my webpage like this :

<div id="JQueryFTD_Demo" class="demo"><ul class="jqueryFileTree" style="">
    <li class="directory collapsed"><a target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="#" rel="/SampleFolder/A/">A</a></li>
    <li class="directory collapsed"><a target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="#" rel="/SampleFolder/B/">B</a></li>
    <li class="directory collapsed"><a target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="#" rel="/SampleFolder/C/">C</a></li>
    <li class="directory collapsed"><a target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="#" rel="/SampleFolder/D/">D</a></li>
    <li class="file ext_accdb"><a target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="#" rel="/SampleFolder/SampleDBFile.accdb">SampleDBFile.accdb</a></li>
    <li class="file ext_txt"><a target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="#" rel="/SampleFolder/SomeText.txt">SomeText.txt</a></li>
</ul></div>

and i try to find tags in it by javascript :

var x = document.getElementsByTagName("a");
return x.textContent;

my return value always is a null, would you please help me?

thanks.

Ad

Answer

Issue is pretty simple ... getElementsByTagname returns a collection that is array like.

You have to iterate the collection to interact with properties of each element instance

Ad
source: stackoverflow.com
Ad