Skip to content

Instantly share code, notes, and snippets.

@jpetitcolas
Created December 28, 2012 06:33
Show Gist options
  • Save jpetitcolas/4395080 to your computer and use it in GitHub Desktop.
Save jpetitcolas/4395080 to your computer and use it in GitHub Desktop.
A short snippet to simulate read-only attribute on a select list, which is currently not W3C-compliant. We can not just disable it, as a disabled input is not sent when submitting a form. The trick here is to store the disabled select value into a hidden field.
/**
* Simulate the read-only attribute on a select list (as it is not normalized).
* @see http://www.w3.org/wiki/HTML/Elements/select
* @param Object Select list element
**/
function setReadOnlyOnSelect(selectList)
{
// Ensure selectList exists
if(!selectList.length) {
console.warn("Unable to find specified select list.");
return;
}
// Create a hidden input to store select value
var input = $('<input />', {
type: "hidden",
name: selectList.attr("name"),
value: selectList.val()
});
// Disable the select
selectList.attr("disabled", true).before(input);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment