How do you make this work? <select id="sel[]">, which is an array of all the dropdown boxes.But if I change $('#sel').change(function() { to $('#sel[]').change(function() {, it does not work?
2 Answers
You can escape the square brackets by two back slashes \\.
$(function() {
$('#sel\\[\\]').change(function() { ... });
});
But ideally you should not use such conventions for naming ids.
1 Comment
escape the special characters used in jQuery selector as per jQuery docs.The string sel[] is not a valid element id:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
Reference
Better to use a simpler id like selArr, for example.
An array of drop-down options will not automatically be created for you. You'll have to maintain the data model yourself, like:
var opts = ['one', 'two', 'three'];
Then add to this array as necessary, and build your drop-down out of it:
This approach keeps the model and view separated, a good programming practice for user interfaces.
1 Comment
id attribute is simply a unique identifier, you'll have to create and maintain an array separately.