Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Moving innerHTML from one page to another, with an external js file.

Hi guys,

I'm practicing making an e-commerce site, and I want to make it so that when you click on an item to add it to basket, the innerHTML of that item is saved to a variable and then appended to a div on another page. So, in brief, it looks something like this:

all the code except from the commented line are a page called cards.php

const addBasket = document.querySelectorAll('.addBasket');

for(let i=0;i<addBasket.length;i++){ addBasket[i].addEventListener('click', function () { cardBlockHTML = addBasket[i].parentElement.innerHTML; console.log(cardBlockHTML);

            basketItems.appendChild(cardBlockHTML); // this is on a different page called basket.php
});

}

Note, the javascript file is inserted in a footer.php file which is added to each page with <?php include("inc/footer.php"); ?>

Any ideas of what I could do?

Best,

P

1 Answer

Steven Parker
Steven Parker
242,296 Points

The click event takes place in the browser, but the files that will construct the other page are stored on the server. So all you can do in your handler is to initiate a postback to the server.

You'll then need code in the server that will handle the incoming postback and do all the work that will affect the contents of the other page.

It's typical for a shopping cart page to be constructed using info in a database that was added by form actions performed on other pages.