Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: this.attr is not a function on class hover

so I have a couple of images, and I want their src to display on another image when it hovers, and then reset back to its original when not hovering anymore. I figured I'd just use $(this), but I get

TypeError: this.attr is not a function

$(".card").hover(
function () {
    var image = $(this.attr('src'));
    $('.cardpreview').attr('src', $(this.attr('src')));
},

function () {
    $('.cardpreviw').attr('src', 'http://mtgimage.com/card/cardback.jpg');
});
.card {
    width:120px;
    height:170px;
}
.cardpreview {
    width:480px;
    height:680px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<img class="card" alt="card missing" src="http://mtgimage.com/card/cardback.jpg" />
<img class="card" alt="card missing" src="http://mtgimage.com/card/cardback.jpg" />
<img class="card" alt="card missing" src="http://mtgimage.com/card/cardback.jpg" />
<img class="card" alt="card missing" src="http://mtgimage.com/card/cardback.jpg" />
<img class="card" alt="card missing" src="http://mtgimage.com/card/cardback.jpg" />
<img class='cardpreview' alt='card missing' src='http://mtgimage.com/card/Craw Wurm.jpg' />

I figure the syntax is probably wrong, while the idea is good. Can anybody help me out?

like image 992
Miha Šušteršič Avatar asked Oct 24 '25 17:10

Miha Šušteršič


1 Answers

this is the native DOMElement. It has no attr() property. You need to convert it to a jQuery object by using $(this) to access jQuery properties and methods. Try this:

$(".card").hover(function () {
  $('.cardpreview').attr('src', $(this).attr('src'));
}, function () {
  $('.cardpreview').attr('src', 'http://mtgimage.com/card/cardback.jpg');
});

Note I also fixed the typo in the second .cardpreview selector.

like image 118
Rory McCrossan Avatar answered Oct 26 '25 07:10

Rory McCrossan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!