I am trying to search my html for class ardiv and then search this class for span.
Then i want to get value from this span element using split function, but i am getting this error:
Uncaught TypeError: paranula.split is not a function
function hledat() {
var divs = document.getElementsByClassName("ardiv");
for (var i = 0; i < divs.length; i++) {
var para = divs[i].getElementsByTagName("span");
var paranula = para[0];
console.log(paranula);
var parasplit = paranula.split(">");
console.log(parasplit[1]);
}
}
hledat();
<span class="hiddenid">188</span>
1 Answer 1
paranula is HTMLElement - kind of JS object, not a string. To access it as a string, use
var parasplit = paranula.outerHTML.split(">");
But if all you need it to take "188" from provided example, use
var result= paranula.innerHTML
The ID of element is other thing - with element like this
<span id="188" class="hiddenid"></span>
you could get "188" with
var result= paranula.id
answered Jun 11, 2017 at 12:35
Michał Sałaciński
2,2761 gold badge13 silver badges11 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
T. Vanasek
Thank you, the ´var result = paranula.innerHTML´ worked.
default
splitfunction and notparanula.id?splitis for splitting strings not selecting/filtering elements or fetching attributes.0th element returned bygetElementByTagNameis not the span you are expecting. Try narrowing down on the selector; maybe anidwould be a better approach.IDwas confusing. Instead of parsing the span HTML as a string, just useparseInt( paranula.innerText )and that will get the id as a number.console.log(document.querySelector('.ardiv .hiddenid').textContent);