You are very close.
$(window).on("resize", function () {
if($(window).width() <= 320) {
$("#mainLink").attr({
'data-toggle': 'collapse',
'data-target': '.navbar-collapse'
});
} else {
$("#mainLink").removeAttr('data-toggle data-target');
}
}
I want to add a couple of attributes to elements with the id of mainLink
You should never have more than one element with the same id. If you intend to have multiple elements with the same id you should change the id to a class.