I have the following javascript that switches the different views. All works fine however it is extremely repetitive.
I would like to clean it up to be optimally usable, but not to sure how I would go about it.
Any help with this would be highly appreciated.
Below is my code that i wrote in order to get it to work. It works well but seems very bloated... don't know what more to say about it but red box telling me to add more text is just not going away, so not to sure how much more i am suppose to type in order to get some assistance with my question at had here.
function switchView () {
var viewContainer = document.getElementById('content-wrapper');
var tileBtn = document.getElementById('tile-btn');
var listBtn = document.getElementById('list-btn');
var swipeBtn = document.getElementById('swipe-btn');
tileBtn.addEventListener( 'click', function(e){
if (viewContainer.classList.contains('list-view') ||
viewContainer.classList.contains('swipe-view') ) {
e.preventDefault();
viewContainer.classList.remove('list-view');
viewContainer.classList.remove('swipe-view');
viewContainer.classList.add('tile-view');
}
});
listBtn.addEventListener( 'click', function(e){
if (viewContainer.classList.contains('tile-view') ||
viewContainer.classList.contains('swipe-view') ) {
e.preventDefault();
viewContainer.classList.remove('tile-view');
viewContainer.classList.remove('swipe-view');
viewContainer.classList.add('list-view');
}
});
swipeBtn.addEventListener( 'click', function(e){
if (viewContainer.classList.contains('list-view') ||
viewContainer.classList.contains('tile-view') ) {
e.preventDefault();
viewContainer.classList.remove('list-view');
viewContainer.classList.remove('tile-view');
viewContainer.classList.add('swipe-view');
}
});
};
switchView();