0

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

    }
}

8
  • Where is your relevant HTML? Please add a full working snippet of your problem. Commented Aug 31, 2020 at 10:41
  • Both Array.from() calls are unnecessary. You can access the elements in a NodeList like in an array (bigRedbtn[0]) or get the length of the list with .length. You could also use NodeList.prototype.forEach() to iterate over the elements in the list: bigRedbtn.forEach((element, index) => { ... }) Commented Aug 31, 2020 at 10:42
  • 1
    Can you please add a minimal reproducible example with an actual description of the requirement because I don't quite get the reason why there are two loops. Commented Aug 31, 2020 at 10:46
  • “I need to replace each bookTitlesArray[j].href with the value of bigRedbtnArray[i].href — what is the expected result? What is each bookTitlesArray[j].href supposed to look like in relation to each bigRedbtnArray[i].href? How does pushing onto an array help with replacing? Commented Aug 31, 2020 at 10:46
  • Thank you for the comments, Andreas, I did try to use forEach, but had to create two forEach func but then was stuck on the replace part. I know how to it like bigRedBtn[0].href = "www.bbc.co.uk" for example but this is just for the one I want to do it for all of the buttons. Thank you! Commented Aug 31, 2020 at 10:50

1 Answer 1

1

S I asked on Reddit and this is the answer I got:

const bigRedbtn = document.querySelectorAll('a.bigRed');
const bookTitles = document.querySelectorAll('.wp-show-posts-entry-title');
bigRedbtn.forEach((n, i) => n.href = bookTitles[i].href)

Thanks to all who helped.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.