0

So I pull from a database a list of items and then display the titles as a list

global.screens.menu.search = function() {
  var searchData = document.getElementById('search-input').value;
  global.method.request('search-item', {title: searchData}, function(err, res) {// if() error handling logic} 
      // show new list of items.
      else {         
      var list = document.createElement('ul');
      for (var i = 0; i < res.list.length; i++) {
        var item = document.createElement('li');
        item.setAttribute('id', 'search-list-' + res.list[i].id);
        item.appendChild(document.createTextNode(res.list[i].title));
        list.appendChild(item);

      }
      document.getElementById('list').appendChild(list);
    }
  })
};

this created a list of items that look like

<li id="search-list-12">the first book</li>
<li id="search-list-16">the fourth book</li>

What I'm trying to do is make each of these items clickable (hyperlink) to a new page called product-page.html and pass the book ID so that I can display the book in the product page by the ID.

I was thinking of just setting session variable to the book ID then setting an href to "localhost://product-page" and on the redirect page load populate the page then delete the session variable. Not sure if that's the right approach.

in my product-page.html I have a function call that will populate everything. I just need to pass it an ID for the item.

global.screens.product.activate(id); // 
veral
  • 67
  • 1
  • 9
  • 1
    I would use a href (as you mentioned ) and pass the id as a parameter using a query string. Heres an example in a situation where the id was 1:
  • the first book
  • – Sarah Jul 27 '19 at 18:48
  • and in product-page.html you can retrieve the query string value using javascript or nodejs. heres a solution on how to retrieve the value using node js. https://stackoverflow.com/questions/6912584/how-to-get-get-query-string-variables-in-express-js-on-node-js – Sarah Jul 27 '19 at 18:54
  • 1
    I'll try this out thank you. I didn't even realize you could use a query string I thought that was just a php thing – veral Jul 27 '19 at 20:13
  • No problem. I hope it works out for you :) – Sarah Jul 28 '19 at 15:36