Created
February 15, 2012 22:52
-
-
Save Lasha/1839785 to your computer and use it in GitHub Desktop.
Dynamically add a <select> menu to an <a> element, make the <select> element itself invisible, but still clickable. Forwards page depending on value of selected <option>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var $changeCountry = $('#change-country'); | |
$changeCountry | |
.find('a') | |
.after(function() { | |
var currentLocation = window.location.hostname, | |
selectHTML = '<ul><li><a href="http://toughmudder.co.uk/">United Kingdom</a></li><li><a href="http://toughmudder.com.au/">Australia</a></li></ul>'; | |
if (currentLocation.search('co.uk') != -1) | |
selectHTML = '<ul><li><a href="http://toughmudder.com/">United States</a></li><li><a href="http://toughmudder.com.au/">Australia</a></li></ul>'; | |
if (currentLocation.search('com.au') != -1) | |
selectHTML = '<ul><li><a href="http://toughmudder.com/">United States</a></li><li><a href="http://toughmudder.co.uk/">United Kingdom</a></li></ul>'; | |
return selectHTML; | |
}); | |
var dropdownPadding = $changeCountry.find('ul').css('padding-top'), | |
dropdownHeight, | |
dropdownCounter = 0; | |
$changeCountry | |
.click(function(e) { e.preventDefault() }) | |
.hover( | |
function () { | |
var $this = $(this).find('ul'); | |
if (dropdownCounter == 0) { | |
dropdownHeight = $this.height(); | |
dropdownCounter++; | |
} | |
$this.stop().animate({ | |
height: 'toggle', | |
paddingTop: dropdownPadding, | |
paddingBottom: dropdownPadding | |
}); | |
}, | |
function () { | |
var $this = $(this).find('ul'); | |
$this.stop().animate({ | |
height: 'toggle', | |
paddingTop: '0px', | |
paddingBottom: '0px' | |
}, { complete: function() { | |
$this.height(dropdownHeight); | |
} | |
}); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment