Created
December 28, 2012 06:33
-
-
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.
This file contains 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
/** | |
* 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