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

131
Visualizações
How can I change the href of a anchor in javascript using query selector

I cannot change an anchor's href when I do it returns saying that there was a null pointer

I tried using

fetch("https://jwapi.herokuapp.com/ck/results")
.then(res => res.json())
.then(data => {
    users = data.map(user => {
        const card = userCardTemplate.content.cloneNode(true).children[0]
        const header = card.querySelector("[data-header]")
        const description = card.querySelector("[data-description]")
        const link = card.querySelector("a[data-link]")
        header.textContent = user.name
        description.textContent = user.description
        link.href = user.link <-- ERROR HERE
        userCardContainer.append(card)
        return { name: user.name, description: user.description, element: card }
    })
    const value2 = searchInput.value
    if (value2 == "") {
        users.forEach(user => {
            user.element.classList.toggle("hide", true)
        })
    }
})

But it gave a error saying

Uncaught (in promise) TypeError: Cannot set properties of null (setting 'href')
at script.js:45:23
at Array.map (<anonymous>)
at script.js:38:22

if needed here is my html

<div class="user-cards" data-user-cards-container></div>
<template data-user-template>
    <a href="#" data-link>
        <div class="card">
            <div class="header" data-header></div>
            <div class="description" data-description></div>
        </div>
    </a>
I tried console.log but and it returned null for card.herf but for user.link it returned the correct one
about 4 years ago · Santiago Gelvez
2 Respostas
Responde à pergunta

0

The issue is that you are trying to find a link inside of card but card does not have a link inside of it. Actually the link contains a card which contains a header and a description.

What is the parent of the link element? Assume it is parent then this should work:

// I used `parent` to get the link, not `card`
const link = parent.querySelector("a[data-link]");

// now link should not be null.
// The reason you get an error is that you try to set href on a null object
link.href = 'http://www.google.com';
about 4 years ago · Santiago Gelvez Relatório

0

You can try using document.querySelector("a").href = "{{url}}";

about 4 years ago · Santiago Gelvez 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