I have a list with several links that contains only the attribute href
:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Home</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<ul>
<li><a href="https:///www.google.com"></a></li>
<li><a href="https:///www.youtube.com"></a></li>
<li><a href="https:///www.twitter.com"></a></li>
</ul>
</body>
</html>
I must obtain the href attribute of each link to put it as the link text, but when I print it in the console it appears as undefined
. In the file script.js
I have:
$(document).ready(function () {
$('a').each((index, value) => {
var enlace = $(this).attr('href');
console.log(enlace);
$(this).text(enlace);
});
});
I expected this result:
But it still shows me empty:
just modify the each of jquery, and with that you have
Try adding a selector to the link container, something like this.
Once with the selector you get all the items inside our container, loop through them and then update them.
Instead of an arrow function you just execute a normal function.
try this