Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

319
Visualizações
(React) How to access a specific element that is being mapped and returned?

I want to access the inner text or inner html of specific cities that I am adding to my page. Since I entered 'moscow' first (then san diego, then oakland) getElementById is only picking up 'moscow'. Based on how my page is set up, I want to access the city name that is next to the 'navigate' button. Clicking on 'oakland' is rendering as 'moscow'. here are some photos: looks like the page is mapping out an array of objects. i want to access oakland if i click on oakland... and my coode shown here, with the .map happening in the return section

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

@Lokendra Soni pointed me in the right direction. I got it to work by swapping .getAttribute("value") with .textContent. But the real trick was to utilize the item's unique ID in their tag's id names (for ex. <p id={displayedCity-${item.id}}>)

about 4 years ago · Juan Pablo Isaza Relatório

0

document.getElementById() always returns the first encountered element matching the provided id, that's why you're receiving Moscow as you've entered it first.

If you want to fetch the data from id attribute, make id of all the items unique.

Try modifying your goToCity method like this:

const goToCity = (id) => {
    const x = document.getElementById(`displayedCity-${id}`).getAttribute("value");
    navigate(`/${x}`);
}

Then modify your HTML:

...
<p id=`displayedCity-${item.id}` value={`YOUR VALUE`}>{item.item}</p>
<button className="button" onClick={()=>goToCity(item.id)}>navigate</button>
...
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda