Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have some div that are:
HTML
<div class="well well-sm" id="skillDIVERTHF"><span >erthf</span><a href="javascript:removeOtherDiv('hSkillItem','skill','erthf','subCatId');"><div class="business_type_close">X</div></a><input type="hidden" value="erthf" name="Hidskill8" id="Hidskill8"></div>

HTML



here the divs class name is same so here i use loop for fetching every divs span text. here is code

JavaScript
var divs = document.getElementsByClassName("well-sm");
		if(divs != null)
		{
			for (var i =0; i<divs.length; i++)
			{
				//according to NodeList
				var strHidValue = divs.item(i).innerHTML;
				var strText = strHidValue.document.getElementsByTagName("span").innerText;
			}
		}



but i can't get span text.this code show an error that is:
JavaScript
Cannot read property 'document' of undefined

plz help me how can i fetch the span text of same naming div.

What I have tried:

var strHidValue = divs.item(i).innerHTML;
var strText = strHidValue.document.getElementsByTagName("span").innerText;
Posted
Updated 15-Jun-16 2:34am

1 solution

The innerHTML property returns a string. Strings do not have a "document" property.

Even if they did, you don't want to fetch all of the <span> elements in the document; you want the elements within the specific <div>.

Also, getElementsByTagName returns a collection of elements, which doesn't have a property called innerText.

Try calling getElementsByTagName on the <div> element:
JavaScript
var divs = document.getElementsByClassName("well-sm");
for (var i = 0; i < divs.length; i++) {
    var div = divs[i];
    var span = div.getElementsByTagName("span")[0];
    if (span) {
        var strText = span.innerText;
        ...
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900