Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

attr is undefined

The following code this not work, because attr is undefined:

$("#foo a[href]").each(function()
{
    this.attr("href", "www.google.com");
});

But this code does:

$("#foo a[href]").each(function()
{
    this.href = "www.google.com";
});

Why??

like image 294
Matt Avatar asked Jan 24 '26 07:01

Matt


1 Answers

You need to wrap this ... $(this)

attr is a method of a jQuery object, href is a property of an element node

like image 188
JaredMcAteer Avatar answered Jan 26 '26 19:01

JaredMcAteer