i have to add attribute rel="sponsored" to all link in page that contain dnlink string. I used the follow code but it doesn't work ...
$("a").each(function() {
link = $(this).attr("href")
if(link.indexOf("dnlink") > -1) {
$(this).attr("rel","sponsored");
}
});
where is the issue?
CodePudding user response:
You haven't declared link
correctly.
Your code before I've submitted this answer:
link = $(this).attr("href")
Should be:
const link = $(this).attr("href");
Look at this:
$("a").each(function() {
const link = $(this).attr("href");
if (link.includes("dnlink")) {
$(this).attr("rel", "sponsored");
$("div").html(link);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="https://example.com/dnlink">dnlink</a>
<a href="https://example.com/not">not dnlink</a>
<div>
</div>