I need some help with this JavaScript code, I'm all new to using javascript so when I get stuck it gets annoying. What I'm trying to do is change the image displayed on my nav bar when on small screens (mobiles) because the nav bar color changes. What I have come up with so far by searching online and some playing only works when the screen size is manually reduced and then the image changes like it should. However I want it to load in this state as well and thats where I am stuck.
$(window).on('resize', function(){
var win = $(this);
if (win.width() < 768) {
$(".navbar-brand img").attr('src', 'img/icons/logowhite.png');
} else {
$(".navbar-brand img").attr('src', 'img/icons/logodark.png');
}});
Could this be done in the css instead is also a question I have?