PAto Hernández PAto Hernández - 27 days ago 6
jQuery Question

Function to simplify code in jquery

I have the following code working:

$(document).ready(function () {
$(".b0").hover(function () {
var src = $(this).attr('data');
var href = $(this).attr('href');
$("#imagen").attr('src', src);
$("#vinculo").attr('href', href);
});
$(".b1").hover(function () {
var src = $(this).attr('data');
var href = $(this).attr('href');
$("#imagen").attr('src', src);
$("#vinculo").attr('href', href);
});
})


But I can't simplify it with a function. I tried several times and ways...

Answer

You can also use this:

 $(document).ready(function () {
        $(".b0 , .b1").hover(function () {
            $("#imagen").attr('src', $(this).attr('data'));
            $("#vinculo").attr('href', $(this).attr('href'));
        });
    })