I currently have a JavaScript file that I'm using to fix the navigation on a website I've built. The code is by no means 'DRY', though, and I'd like to fix that issue.
//fixing the nav dropdowns for mobile
//concrete and joint repair dropdown section
$(document).ready(function() {
// Optimalisation: Store the references outside the event handler:
var $window = $(window);
function checkWidth() {
var windowsize = $window.width();
if (windowsize < 990) {
//if the window is less than 990px wide then turn on flyout on click..
$("#toggle").click(function(){
var $target = $('.target'),
$toggle = $(this);
$target.toggle();
});
}
else if (windowsize > 990) {
//if the window is less than 990px wide then flyout on hover..
$("#toggle").hover(function(){
var $target = $('.target'),
$toggle = $(this);
$target.toggle();
});
$(".target").hover(function(){
var $target = $('.target'),
$toggle = $(this);
$target.toggle();
});
}
}
// Execute on load
checkWidth();
// Bind event listener
$(window).resize(checkWidth);
});
//end concrete and joint repair dropdown section
//primers and sealers dropdown section
$(document).ready(function() {
// Optimalisation: Store the references outside the event handler:
var $window = $(window);
function checkWidth() {
var windowsize = $window.width();
if (windowsize < 990) {
//if the window is less than 990px wide then turn on flyout on click..
$("#toggleTwo").click(function(){
var $target = $('.targetTwo'),
$toggle = $(this);
$target.toggle();
});
}
else if (windowsize > 990) {
//if the window is less than 990px wide then flyout on hover..
$("#toggleTwo").hover(function(){
var $target = $('.targetTwo'),
$toggle = $(this);
$target.toggle();
});
$(".targetTwo").hover(function(){
var $target = $('.targetTwo'),
$toggle = $(this);
$target.toggle();
});
}
}
// Execute on load
checkWidth();
// Bind event listener
$(window).resize(checkWidth);
});
//end primers and sealers dropdown section
//high build coatings dropdown section
$(document).ready(function() {
// Optimalisation: Store the references outside the event handler:
var $window = $(window);
function checkWidth() {
var windowsize = $window.width();
if (windowsize < 990) {
//if the window is less than 990px wide then turn on flyout on click..
$("#toggleThree").click(function(){
var $target = $('.targetThree'),
$toggle = $(this);
$target.toggle();
});
}
else if (windowsize > 990) {
//if the window is less than 990px wide then flyout on hover..
$("#toggleThree").hover(function(){
var $target = $('.targetThree'),
$toggle = $(this);
$target.toggle();
});
$(".targetThree").hover(function(){
var $target = $('.targetThree'),
$toggle = $(this);
$target.toggle();
});
}
}
// Execute on load
checkWidth();
// Bind event listener
$(window).resize(checkWidth);
});
//end high build coatings dropdown section
//self levelers and overlays dropdown section
$(document).ready(function() {
// Optimalisation: Store the references outside the event handler:
var $window = $(window);
function checkWidth() {
var windowsize = $window.width();
if (windowsize < 990) {
//if the window is less than 990px wide then turn on flyout on click..
$("#toggleFour").click(function(){
var $target = $('.targetFour'),
$toggle = $(this);
$target.toggle();
});
}
else if (windowsize > 990) {
//if the window is less than 990px wide then flyout on hover..
$("#toggleFour").hover(function(){
var $target = $('.targetFour'),
$toggle = $(this);
$target.toggle();
});
$(".targetFour").hover(function(){
var $target = $('.targetFour'),
$toggle = $(this);
$target.toggle();
});
}
}
// Execute on load
checkWidth();
// Bind event listener
$(window).resize(checkWidth);
});
//end self levelers and overlays dropdown section
//vertical coatings dropdown section
$(document).ready(function() {
// Optimalisation: Store the references outside the event handler:
var $window = $(window);
function checkWidth() {
var windowsize = $window.width();
if (windowsize < 990) {
//if the window is less than 990px wide then turn on flyout on click..
$("#toggleFive").click(function(){
var $target = $('.targetFive'),
$toggle = $(this);
$target.toggle();
});
}
else if (windowsize > 990) {
//if the window is less than 990px wide then flyout on hover..
$("#toggleFive").hover(function(){
var $target = $('.targetFive'),
$toggle = $(this);
$target.toggle();
});
$(".targetFive").hover(function(){
var $target = $('.targetFive'),
$toggle = $(this);
$target.toggle();
});
}
}
// Execute on load
checkWidth();
// Bind event listener
$(window).resize(checkWidth);
});
//end vertical coatings dropdown section
//performance topcoats dropdown section
$(document).ready(function() {
// Optimalisation: Store the references outside the event handler:
var $window = $(window);
function checkWidth() {
var windowsize = $window.width();
if (windowsize < 990) {
//if the window is less than 990px wide then turn on flyout on click..
$("#toggleSix").click(function(){
var $target = $('.targetSix'),
$toggle = $(this);
$target.toggle();
});
}
else if (windowsize > 990) {
//if the window is less than 990px wide then flyout on hover..
$("#toggleSix").hover(function(){
var $target = $('.targetSix'),
$toggle = $(this);
$target.toggle();
});
$(".targetSix").hover(function(){
var $target = $('.targetSix'),
$toggle = $(this);
$target.toggle();
});
}
}
// Execute on load
checkWidth();
// Bind event listener
$(window).resize(checkWidth);
});
//end performance topcoats dropdown section
//elastomerics dropdown section
$(document).ready(function() {
// Optimalisation: Store the references outside the event handler:
var $window = $(window);
function checkWidth() {
var windowsize = $window.width();
if (windowsize < 990) {
//if the window is less than 990px wide then turn on flyout on click..
$("#toggleSeven").click(function(){
var $target = $('.targetSeven'),
$toggle = $(this);
$target.toggle();
});
}
else if (windowsize > 990) {
//if the window is less than 990px wide then flyout on hover..
$("#toggleSeven").hover(function(){
var $target = $('.targetSeven'),
$toggle = $(this);
$target.toggle();
});
$(".targetSeven").hover(function(){
var $target = $('.targetSeven'),
$toggle = $(this);
$target.toggle();
});
}
}
// Execute on load
checkWidth();
// Bind event listener
$(window).resize(checkWidth);
});
//end elastomerics dropdown section
//cleaning products dropdown section
$(document).ready(function() {
// Optimalisation: Store the references outside the event handler:
var $window = $(window);
function checkWidth() {
var windowsize = $window.width();
if (windowsize < 990) {
//if the window is less than 990px wide then turn on flyout on click..
$("#toggleEight").click(function(){
var $target = $('.targetEight'),
$toggle = $(this);
$target.toggle();
});
}
else if (windowsize > 990) {
//if the window is less than 990px wide then flyout on hover..
$("#toggleEight").hover(function(){
var $target = $('.targetEight'),
$toggle = $(this);
$target.toggle();
});
$(".targetEight").hover(function(){
var $target = $('.targetEight'),
$toggle = $(this);
$target.toggle();
});
}
}
// Execute on load
checkWidth();
// Bind event listener
$(window).resize(checkWidth);
});
//end cleaning products dropdown section
//installations dropdown section
$(document).ready(function() {
// Optimalisation: Store the references outside the event handler:
var $window = $(window);
function checkWidth() {
var windowsize = $window.width();
if (windowsize < 990) {
//if the window is less than 990px wide then turn on flyout on click..
$("#toggleNine").click(function(){
var $target = $('.targetNine'),
$toggle = $(this);
$target.toggle();
});
}
else if (windowsize > 990) {
//if the window is less than 990px wide then flyout on hover..
$("#toggleNine").click.hover(function(){
var $target = $('.targetNine'),
$toggle = $(this);
$target.toggle();
});
$(".targetNine").click.hover(function(){
var $target = $('.targetNine'),
$toggle = $(this);
$target.toggle();
});
}
}
// Execute on load
checkWidth();
// Bind event listener
$(window).resize(checkWidth);
});
//end installations dropdown section
//additional products dropdown section
$(document).ready(function() {
// Optimalisation: Store the references outside the event handler:
var $window = $(window);
function checkWidth() {
var windowsize = $window.width();
if (windowsize < 990) {
//if the window is less than 990px wide then turn on flyout on click..
$("#toggleTen").click(function(){
var $target = $('.targetTen'),
$toggle = $(this);
$target.toggle();
});
}
else if (windowsize > 990) {
//if the window is less than 990px wide then flyout on hover..
$("#toggleTen").hover(function(){
var $target = $('.targetTen'),
$toggle = $(this);
$target.toggle();
});
$(".targetTen").hover(function(){
var $target = $('.targetTen'),
$toggle = $(this);
$target.toggle();
});
}
}
// Execute on load
checkWidth();
// Bind event listener
$(window).resize(checkWidth);
});
//end additional products dropdown section
//end fixing the nav dropdowns for mobile