How to send id as parameter to url

Asked

Viewed 125 times

-2

I am training to build a blog, I can already recover the posts via API and display the results on the screen, but I need now when you click on the item (post), its id is sent to the url, forming what you ask in the API to display the details of that item.

It’s like this...

    const areaPostagens = document.querySelector('.main');

    listagem().then((posts) => {  // Retorno do Fetch

       posts.forEach((post) => {

          areaPostagens.appendChild( exibePostagem(post) );

       })
    })

    exibePostagem = (post) => {
       console.log(post)

       const article = document.createElement('article');
       article.classList = 'post'

       const postagem = 
       `
          <a href="leitura.html/${post._id}" class="container-post">
             <h2 class="titulo">${post.titulo}</h2>
             <p class="paragrafo">${post.paragrafo.substr(0, 250)}...</p>
          </a>

          <footer class="rodape-post">
             <span>${post.data}</span>
          </footer>

          <hr>
       `
       article.innerHTML = postagem

       return article

    }

1 answer

0

You can try it like this:

...
const postUrl = `${window.location.pathname}/${post._id}`;

const postagem = 
       `
          <a href=${postUrl} class="container-post">
             <h2 class="titulo">${post.titulo}</h2>
...

If it doesn’t work, explore another property of window.location. Worth reading the API as well, see here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.