element.childElementCount is return incorrect value

The name of the picture


element.childElementCount is return incorrect value



I want to select element have no child.



This is js code




var elements = document.body.getElementsByTagName("*");

elements.each(function(element) {
if (element.childElementCount == 0) {
element.addEventListener(
"click",
function() {
makeBorder(selectedElement, false);
selectedElement = element;
makeBorder(element, true);
},
false
);
} else {
element.removeEventListener("click", null, false);
element.removeEventListener("onclick", null, false);
}
});



From link "m.naver.com", class["grid1 id_cui_cupid_news _MM_AIRS_CONTAINER"]'s real childElementCount is not 0.



But in code, childElementCount is 0.



I'm running this code in android webview.



How can I get the element that has no child?



reference : Select element inside WebView and get details



[[[[[[[ Edited ]]]]]]




Object.prototype.each = function(fn, bind) {
for (var i = 0; i < this.length; i++) {
if (i in this) {
fn.call(bind, this[i], i, this);
}
}
};

var selectedElement = null;

document.getElementsByTagName("*").addEventListener("click", function(e) {
if(e.target && e.target.childElementCount == 0) {

makeBorder(selectedElement, false);
selectedElement = element;
makeBorder(element, true);
}
});

// here

var makeBorder = function(element, selected) {
if (element) {
element.style.cssText = selected ? 'background: #CDEA90;' : '';
}
};





Are you missing a library (or maybe mispelt forEach) as your example doesn't work.
– evolutionxbox
Jul 17 at 9:08


forEach





@evolutionxbox oh...reference link is incorrect. I have changed the link. there is no library. only javascript.
– 01hanst
Jul 17 at 9:16





elements.each will still throw an error.
– evolutionxbox
Jul 17 at 10:11


elements.each





@evolutionxbox but i can get element in function of addEventListener(). sometimes element.childElementCount is 0. but it have children.
– 01hanst
Jul 17 at 10:27





No I mean it elements.each is not a function
– evolutionxbox
Jul 17 at 10:42


elements.each is not a function









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to scale/resize CVPixelBufferRef in objective C, iOS

Stripe::AuthenticationError No API key provided. Set your API key using “Stripe.api_key = ”

SVG with two text elements. When one resizes due to textLength - how to resize the other one to the same character size