Update here is a link of a working copy in a sandbox:
https://codesandbox.io/s/3pkzc
So I am trying to run this code - trying to use the DOM to get index .href value and replace it.
But I am stuck at the last part of actually replacing the value [j] index with the value [i] index.
I've tried to push it on as it is an array and researched other methods but I am getting no where with this.
// Get the URL of the big red button
const bigRedbtn = document.querySelectorAll('.bigRed');
//Convert into an array
const bigRedbtnArray = Array.from(bigRedbtn);
//Get all the links
const bookTitles = document.querySelectorAll('.wp-show-posts-entry-title a');
console.log(bookTitles.length);
//Convert into an array
const bookTitlesArray = Array.from(bookTitles);
console.log(bookTitlesArray.length);
for (var i = 0; i < bigRedbtnArray.length; i++) {
for (var j = 0; j < bookTitlesArray.length; j++) {
// what is the links for the book?
const link = bigRedbtnArray[i].href;
console.log(link);
// console.log('bigRedbtn: ', bigRedbtn[i].href);
//this is the title url to be replaced with the link from bigRedBtn
const titleLink = bookTitlesArray[j].href;
console.log(titleLink);
//So can you do something like this
//This is where I am stuck at
//I need to replace each bookTitlesArray[j].href with the value of bigRedbtnArray[i].href
}
}
Array.from()calls are unnecessary. You can access the elements in aNodeListlike in an array (bigRedbtn[0]) or get the length of the list with.length. You could also useNodeList.prototype.forEach()to iterate over the elements in the list:bigRedbtn.forEach((element, index) => { ... })bookTitlesArray[j].hrefwith the value ofbigRedbtnArray[i].href” — what is the expected result? What is eachbookTitlesArray[j].hrefsupposed to look like in relation to eachbigRedbtnArray[i].href? How does pushing onto an array help with replacing?