I'm trying to reduce the amount of JS that I have and I'm not sure why I'm not allowed to do it the way I'm trying. It's just this code for some mouseover/mouseout functions on images:
$(document).ready(function() {
$("#social_btn_1").on('mouseover', function(){
$(this).attr('src', 'images/social_1_dwn.png');
})
$("#social_btn_1").on('mouseout', function(){
$(this).attr('src', 'images/social_1.png');
})
$("#social_btn_2").on('mouseover', function(){
$(this).attr('src', 'images/social_2_dwn.png');
})
$("#social_btn_2").on('mouseout', function(){
$(this).attr('src', 'images/social_2.png');
})
$("#social_btn_3").on('mouseover', function(){
$(this).attr('src', 'images/social_3_dwn.png');
})
$("#social_btn_3").on('mouseout', function(){
$(this).attr('src', 'images/social_3.png');
})
$("#social_btn_4").on('mouseover', function(){
$(this).attr('src', 'images/social_4_dwn.png');
})
$("#social_btn_4").on('mouseout', function(){
$(this).attr('src', 'images/social_4.png');
})
});
And I'm trying to shorten by doing this:
$(document).ready(function() {
for (var i = 1; i < 5; i++) {
$("#social_btn_"+ i).on('mouseover', function(){
$("#social_btn_"+ i).attr('src', 'images/social_'+ i +'_dwn.png');
})
$("#social_btn_"+ i).on('mouseout', function(){
$("#social_btn_"+ i).attr('src', 'images/social_'+ i +'.png');
})
}
});
Can anyone explain why this doesn't work and what is the best way to consolidate the code I have? (I know you can do this stuff with CSS3 by the way, but I need to use JQuery). Thanks.