Returning Text in Multiple Elements as an Array Using JavaScript

#2 Methods of Returning Text in Multiple Elements as an Array Using JavaScript

Method 1 – Using Array function in javascript

var method1 = Array.from(document.getElementsByClassName(“reviewAuthor”)).map(el => el.innerText);

Method 2 – using For Loop function

In javascript to add condition of only returning elements that text is equal to £0.00. The “for” loop is needed because otherwise it would only return one element on the page meeting the criteria.

var elements = document.getElementsByClassName(‘product-price__price’);
var soldOut = document.getElementsByClassName(‘product-price__sold-out’);

for (var i = 0; i < elements.length; i++) {
if (elements[i].innerHTML == “£0.00”) {
elements[i].innerHTML = “Enquire”;
soldOut[i].style.display = ‘none’;
}
}

Leave a Reply