@ m3g4p0p close one. I did use every and some other method (filter, etc.) to target the items that do not have the specific marks/attributes.
The thing is I need to do this in a loop. It’s hard to explain (I have edited the question with HTML variable).
@ m_hutley
Be very careful here, guys. title
is a special attribute for HTML elements - javascript (at least in Chrome) considers all links to have a title, and that it is the empty string if not specified, so undefined
will never be true.
Got it. Didn’t know of this behavior, thanks! I have edited the question.
I would suggest a simpler method: Don’t loop at all until you’re sure.
Yea but then we are looping twice - once to get all element that does not meet some standard, and again through all elements. I need to use this loop cause it have some async operations later on, and they need to target the data (added HTML variable in this example) that this loop passed in a specific order.
If the length of the return = the length of the return from the getByTagName, you do your thing.
This is exactly what I did.
@ coothead nice. But there can more links without title then one. If
if ( count ) {
doStuff( count );
}
would be putted inside for loop, then it would work I suppose, if I understand this correctly of course.
EDIT: Maybe I will just post code here (did add many console.log to see running results):
let links = document.getElementsByTagName('a');
console.log('======================');
console.log('links HTML collection:\n');
console.log(links);
console.log('======================');
let linksLength = links.length;
let linksTitleCount = 0;
let html = '';
let linksArray = Array.from(links); // converting HTML collection to an array
let allLinksThatDoesHaveTitleAttr = linksArray.filter(function(l)
{
return (l.getAttribute('title') !== undefined && l.getAttribute('title') !== null && l.getAttribute('title') !== '');
});
let allLinksThatDoesHaveTitleAttrCount = allLinksThatDoesHaveTitleAttr.length;
console.log();
console.log('======================');
console.log('allLinksThatDoesHaveTitleAttribute:\n');
console.log(allLinksThatDoesHaveTitleAttrCount);
console.log('======================');
for (i = 0; i < linksLength; i++) { // main loop throught ALL links
// lets supose we... build html element
html += 'link' + [i] +'\n';
if (links[i].getAttribute('title') !== undefined && links[i].getAttribute('title') !== null && links[i].getAttribute('title') !== '') { // condition that check existance of title attribute - thanks @m_hutley
linksTitleCount += 1; // this is cause I need to count all loop part that fulfil the condition
console.log(linksTitleCount);
console.log();
html += 'this link - ' + [i] + ' - does not have title attribute! \n\n';
if (linksTitleCount == allLinksThatDoesHaveTitleAttrCount)
{
console.log('DONE!'); // we know we are done - we loop throught all elements that does have title attr
// NOW if it's the last item that fulfil this condition then do something (in this main loop) + some async operations that will leater (when data arrive) target specific HTML with specific link (order like in loop - [i])
}
}
}
console.log('HTML result is:\n');
console.log(html);