Skip to content

Instantly share code, notes, and snippets.

@Lasha
Created February 15, 2012 22:52
Show Gist options
  • Save Lasha/1839785 to your computer and use it in GitHub Desktop.
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>
$('#change-country')
.click(function(e) { e.preventDefault() })
.append(function() {
var currentLocation = window.location.hostname,
selectHTML = '<select><option value="">Choose Country:</option><option value="http://toughmudder.co.uk/">United Kingdom</option><option value="http://toughmudder.com.au/">Australia</option></select>';
if (currentLocation.search('co.uk') != -1)
selectHTML = '<select><option value="">Choose Country:</option><option value="http://toughmudder.com/">United States</option><option value="http://toughmudder.com.au/">Australia</option></select>';
if (currentLocation.search('com.au') != -1)
selectHTML = '<select><option value="">Choose Country:</option><option value="http://toughmudder.com/">United States</option><option value="http://toughmudder.co.uk/">United Kingdom</option></select>';
return selectHTML;
})
.find('select')
.on('change', function () {
var $this = $(this); // references the <select> element
var countryName = $this.find('option:selected').text(); // get selected text (country name)
var url = $this.val(); // get selected value
$this.parent().text(countryName); // $this.parent reference the parent anchor, replaces text/menu with selected country's name
if (url != "") { window.location = url } // forward if url is not blank/empty
return false;
})
.css({
'height': function() {
return $(this).parent().outerHeight();
},
'width': function() {
return $(this).parent().outerWidth();
}
});
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);
}
});
}
);
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