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

165
Visualizações
Find Specific URL based link and add extra value in rel attributes into full website via jquery

I want to add extra value into attributes where all links were rel='nofollow' and href='example.com'.For more details, I attach my code below.

My Expected Output:

<a target="_blank" rel="nofollow sponsored" class="" href="https://amzn.to/tes" style="width: 100%;">Aff Link</a>
<a target="_blank" rel="follow" href="example.com">Follow</a>

My code Below:

var test = jQuery('a').attr('href').split('/')[2];

if (test == 'amzn.to') {
  jQuery('a').attr('rel', 'nofollow sponsored');
  console.log(test);
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>jQuery Rel Attribute</title>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>

<body>
  <a href="example.com">Example link</a> <br>
  <a target="_blank" rel="nofollow" class="" href="https://amzn.to/tessdft" style="width: 100%;">
        Link 1
    </a> <br>
  <a target="_blank" rel="nofollow" class="" href="https://amzn.to/testws" style="width: 100%;">
        Link 2
    </a> <br>
  <a target="_blank" rel="nofollow" class="" href="https://amzn.to/testee" style="width: 100%;">
        Link
    </a> <br>

</body>

</html>

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

0

Your line

jQuery('a').attr('href')

will only return the href for the first a - you need to loop each a:

console.log("before: " + $("a[rel*=sponsored]").length)

$("a").each(function() {
  var test = $(this).attr('href').split('/')[2];

  if (test == 'amzn.to') {
    $(this).attr('rel', 'nofollow sponsored');
    console.log(test);
  }
});

console.log("after: " + $("a[rel*=sponsored]").length)
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<a href="example.com">Example link</a> <br>
<a target="_blank" rel="nofollow" href="https://amzn.to/tessdft">Link 1</a><br>
<a target="_blank" rel="nofollow" href="https://amzn.to/testws">Link 2</a><br>
<a target="_blank" rel="nofollow" href="https://amzn.to/testee">Link3</a><br>

There is also an overload for .attr which allows you to pass a function

console.log("before: " + $("a[rel*=sponsored]").length)

$("a").attr("rel", function() {
  var test = $(this).attr('href').split('/')[2];
  if (test == 'amzn.to') {
    return 'nofollow sponsored';
  }
});

console.log("after: " + $("a[rel*=sponsored]").length)
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<a href="example.com">Example link</a> <br>
<a target="_blank" rel="nofollow" href="https://amzn.to/tessdft">Link 1</a><br>
<a target="_blank" rel="nofollow" href="https://amzn.to/testws">Link 2</a><br>
<a target="_blank" rel="nofollow" href="https://amzn.to/testee">Link3</a><br>

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